Unity 2D 在移动时将汽车对象旋转到下一个目标点

问题描述 投票:0回答:0

在 2D Unity 项目中,我有一辆汽车朝目标点移动,我需要汽车面向下一个目标点。

这里是动车代码。

public class gr_car_move : MonoBehaviour
{
    public float speed;
    public Transform[] targets;

    private int currentTargetIndex = 0;

    // Start is called before the first frame update
    void Start()
    {
        if (targets.Length == 0)
        {
            Debug.LogError("No targets assigned to script!");
        }
    }

    // Update is called once per frame
    void Update()
    {
        var step = speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, targets[currentTargetIndex].position, step);

        if (transform.position == targets[currentTargetIndex].position)
        {
            if (currentTargetIndex == targets.Length - 1)
            {
                // reached last target, stop cycling
                return;
            }
            currentTargetIndex = (currentTargetIndex + 1) % targets.Length;
        } 


    }
} 
unity-game-engine 2d
© www.soinside.com 2019 - 2024. All rights reserved.