gpt4 book ai didi

c# - 如何在unity3d中制作测距仪

转载 作者:行者123 更新时间:2023-11-30 16:46:23 25 4
gpt4 key购买 nike

我正在尝试制作测距仪。但我没有得到我想要的。所以我上传了一张照片。我想要弯曲的距离,但我正在变直,比如位移。

enter image description here

这是我的代码:

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";
}
}

最佳答案

您可以累加行驶距离以找出曲线上的行驶距离:

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";
}
}

但为了找出剩余距离(或尚未移动的位移),您至少需要知道曲线的半径:

我假设曲线是圆的一部分

如果曲线是半个圆,位移是半径×pi

否则如果已知角度,则位移为半径×角度(弧度)

如果角度未知但半径和距离已知,则位移为2 × radius × arcsin(distance/2 × radius)

enter image description here

关于c# - 如何在unity3d中制作测距仪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40438507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com