微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有没有办法使用刚体将游戏对象从 a 点移动到 b 点和 b 点?

如何解决有没有办法使用刚体将游戏对象从 a 点移动到 b 点和 b 点?

我正在将一个游戏对象从一个点移动到另一个点。使用刚体,我使用此代码将游戏对象从一个点移动到另一个点。但不是正确的方法,因为有时我必须停止对象。这段代码不允许这种情况发生。我正在使用时间。是时候继续前进了。

private void Start()
{
    rb = GetComponent<Rigidbody>();
    pusherinitPos = transform.position;
}

private void FixedUpdate()
{
    if (!stopMove)
    {
        float timeSin = Mathf.Sin(Time.time) / divider;
        Vector3 newPos = new Vector3(pusherinitPos.x,pusherinitPos.y,pusherinitPos.z + timeSin);
        rb.MovePosition(newPos);
    }
}

解决方法

Time.time 当然会在您的对象停止时继续运行。

你宁愿使用

// store your own passed time
private float _time;

private void Start()
{
    rb = GetComponent<Rigidbody>();
    pusherinitPos = transform.position;
}

private void FixedUpdate()
{
    if (stopMove) return;

    var timeSin = Mathf.Sin(_time) / divider;
    var newPos = pusherinitPos + Vector3.forward * timeSin;
    rb.MovePosition(newPos);

    // increase by the time passed since last frame
    // see https://docs.unity3d.com/ScriptReference/Time-deltaTime.html
    _time += Time.deltaTime;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。