2016-11-05 23 views
3

我正在嘗試製造測距儀。但我沒有得到我想要的東西。所以我上傳了一張圖片。我想彎曲的距離,但我得到像移位一樣的直線。如何在unity3d中製造測距儀

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

我建議您問這個問題在math.stackexchange.com – Bijan

+0

這個問題似乎有點不完整的,你的移動物體沿曲線或其它什麼是曲線的半徑? – Bijan

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

但爲了找出剩餘的距離(或尚未行進的位移)至少需要知道曲線的半徑:

我假定曲線是圓

如果曲線是圓的一半的一部分,該位移是半徑×PI

否則如果天使是已知的,位移半徑×天使(弧度)

如果天使是未知的,但半徑和距離是已知的,位移是2×半徑×反正弦(距離/ 2×半徑)

enter image description here

+0

好的,謝謝你的回答,我明白這一點。但如果有像波浪或道路曲線之類的不規則曲線或類似的東西呢? –

+0

在這種情況下,您需要近似。或者您可以使用樣條曲線和曲線等庫來沿着路徑移動對象。這些方法中的值是指路徑上的位移。只是谷歌統一樣條路徑 – Bijan

+0

其實我應該寫在問題上,對不起,我錯過了。我正在使用Spline移動對象。 Hermite樣條控制器。 –