根据Unity的距离改变速度?

时间:2015-10-09 22:10:14

标签: c# unity3d game-physics

我正在使用var heading = target.position - player.position;计算一个Vector3,它显示从我的敌人到我的玩家的标题,但显然当距离不同时速度会有所不同。因此,当它真的很接近它会变得非常缓慢,当它很远时它将会非常快。有没有办法让它一直保持同样的速度?我正在使用rigidbody.velocity btw。

这是向玩家脚本的全面转变:

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 个答案:

答案 0 :(得分: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

我希望,这有助于

相关问题