为什么重力脚本将物体拉得如此之快然后变慢,而不是恒定的

Why does gravity script pull objects so fast then slows, not constant

如何为这个重力脚本获得恒定速度。物体开始时非常快,然后在接近目标时减速。我只是想要一个恒定的速度,但我认为我已经实现了。

private Rigidbody ownBody;
private float maxVelocityOfInfluence = 10.2f;
private float rangeOfInfluence = 20000.0f;
public bool allowRangeGizmo = false;

private void OnEnable()
{
    ownBody = GetComponent<Rigidbody>();
}

private void FixedUpdate()
{
    Collider[] cols = Physics.OverlapSphere(transform.position, rangeOfInfluence);
    List<Rigidbody> rbs = new List<Rigidbody>();

    foreach (Collider c in cols)
    {
        Rigidbody otherBody = c.attachedRigidbody;
        if (otherBody != null && otherBody != ownBody && !rbs.Contains(otherBody) && (otherBody.transform.gameObject.tag == "Enemy1" || otherBody.transform.gameObject.tag == "Enemy2" || otherBody.transform.gameObject.tag == "Enemy3" || otherBody.transform.gameObject.tag == "Enemy4"))
        {
            rbs.Add(otherBody);

            Vector3 offset = transform.position - c.transform.position;
            otherBody.AddForce(offset * ownBody.mass);

            //  Control speed of Enemy
            if (otherBody.velocity.magnitude > maxVelocityOfInfluence)
            {
                otherBody.velocity = otherBody.velocity.normalized * maxVelocityOfInfluence;
            }
        }
    }
}

难道是这段代码?偏移量似乎改变了速度?

Vector3 offset = transform.position - c.transform.position;
otherBody.AddForce(offset * ownBody.mass);

是的,你离得越远,偏移值就越大,所以增加的力也会越大。您可以将偏移向量归一化,使其成为方向向量 (offset.normalized * ownBody.mass).