2015-10-09 57 views
0

我使用var heading = target.position - player.position;來計算一個Vector3,它顯示了從我的敵人到我的玩家的標題,但顯然當距離不同時速度不同。所以當它真的很近的時候,它會變得非常慢,當它很遠的時候它會變得非常快。有沒有辦法讓它始終保持相同的速度?我使用rigidbody.velocity順便說一句。速度取決於Unity中的距離?

這是對球員腳本全招:

using UnityEngine; 
using System.Collections; 

public class Movement : MonoBehaviour { 
public Rigidbody rb; 
public bool yaonah = true; 
public Transform player; 
public float speed = 100; 

// Use this for initialization 
void Start() { 
    rb = GetComponent<Rigidbody>(); 

} 

// Update is called once per frame 
void Update() { 
    if (yaonah) { 
     StartCoroutine(Wait(2)); 
    } 

} 

void Move() 
{ 
    Debug.Log(rb.velocity); 
    //Vector3 dir = Random.insideUnitCircle * 5; 
    var pdir = player.position - transform.position; 
    rb.velocity = pdir; //Player Direction 
    Debug.Log("HEY"); 
    Debug.Log(pdir + "PlayerPos"); 
} 
IEnumerator Wait(float waittime) 
{ 
    yaonah = false; 
    Move(); 
    yield return new WaitForSeconds(waittime); 
    yaonah = true; 
} 
} 
+1

你有更多的代碼w e可以用來使這個學習過程? – ejsd1989

回答

0

因此而不是使用

var pdir = player.position - transform.position; 
rb.velocity = pdir; //Player Direction 

嘗試使用這樣的:

var pDir = player.position - transform.position; 
pDir = (1f/pDir.magniude()) * pDir; //This Vector3 has now the Length 1 
rb.velocity = pDir; 
//if you want to make him faster, 
//multiply this vector with some value of your choice 

我希望這會有所幫助