速度变化取决于 Unity 中的距离?

Speed change depending on distance in Unity?

我正在使用 var heading = target.position - player.position; 来计算显示从我的敌人到我的玩家的航向的 Vector3,但显然当距离不同时速度也不同。所以当它真的很近时,它会走得很慢,而当它很远时,它会走得很快。有没有办法让它一直保持相同的速度?我正在使用 rigidbody.velocity 顺便说一句。

这是播放器脚本的完整步骤:

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

所以不用

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

希望对您有所帮助