【问题标题】:Unity Moving Platform script stops working?Unity 移动平台脚本停止工作?
【发布时间】:2019-12-03 02:35:54
【问题描述】:

我有一个脚本应该在 2 个位置之间移动平台。 平台移动到 pos1,但随后停止....

这是我的代码: 使用 System.Collections; 使用 System.Collections.Generic; 使用 UnityEngine;

public class Platform : MonoBehaviour
{
    public Transform pos1, pos2;

    public float speed;

    public Transform startPos;

    Vector3 nextPos;


    // Start is called before the first frame update
    void Start()
    {
        nextPos = pos1.position;
    }

    // Update is called once per frame
    void Update()
    {

        if (transform.position == pos1.position)
        {
            nextPos = pos2.position;
        }
        if (transform.position == pos2.position)
        {
            nextPos = pos1.position;
        }

        transform.position = Vector3.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);

    }

    private void OnDrawGizmos()
    {
        Gizmos.DrawLine(pos1.position, pos2.position);
    }
}

【问题讨论】:

    标签: c# unity3d 2d


    【解决方案1】:

    您是按顺序设置的,所以它始终是第二个选项。

    在一种情况下,如果nextpos 在position 1 中,它将被设置为位置 2,但随后会立即再次更改为 如果nextpos 在position 2 中,它将被设置回1。

    简单的更改应该只是将第二个if 语句更改为else if

    解释代码,但希望它有意义

    【讨论】:

    • 另外最好使用 Vector3 类而不是 Transform 类来持有位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 2015-12-03
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多