我正在尝试制作一个测距仪。但我没有得到我想要的东西。所以我上传了一张照片。我想弯曲的距离,但我正在变得像移位。
这是我的代码:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class DistanceMeter : MonoBehaviour {
public GameObject current;
public GameObject destination;
private float beginPos;
private float targetPos;
private float currPos;
public Text dist;
void Start()
{
beginPos = current.transform.position.x;
targetPos = destination.transform.position.x;
}
void Update()
{
currPos = current.transform.position.x - targetPos;
int Distance = Mathf.Abs(Mathf.RoundToInt (currPos));
dist.text = Distance.ToString()+ " meters";
}
}
答案 0 :(得分:2)
您可以累积行进距离以找出曲线上的行进距离:
public class DistanceMeter : MonoBehaviour
{
public GameObject current;
public GameObject destination;
private float beginPos;
private float targetPos;
private float currPos;
private float displacement;
public Text dist;
void Start()
{
beginPos = current.transform.position.x;
targetPos = destination.transform.position.x;
}
void Update()
{
float currDelta = current.transform.position.x - currPos;
currPos = current.transform.position.x;
displacement += Mathf.Abs(currDelta);
dist.text = displacement.ToString() + " meters";
}
}
但是为了找出剩余的距离(或尚未行进的位移),您至少需要知道曲线的半径:
我认为曲线是圆圈的一部分
如果曲线是圆的一半,则位移 radius×pi
如果已知天使,则位移为半径×天使(以弧度表示)
如果天使未知但半径和距离已知,则位移 2×radius×arcsin(distance / 2×radius)