【问题标题】:Speed change depending on distance in Unity?速度变化取决于Unity中的距离?
【发布时间】:2015-10-09 22:10:14
【问题描述】:

我正在使用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;
}
}

【问题讨论】:

  • 你有更多的代码我们可以用来学习吗?

标签: c# unity3d game-physics


【解决方案1】:

所以而不是使用

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

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多