Unity2D物体按照指定路径不断移动

今天在设计一个NPC时想让NPC沿着指定路径移动,可以使用MoveTowards以及MovePosition函数,需要在物体上加上RigidBody2D

public class EnemyMove : MonoBehaviour
{
    public int index = 0;                       //从初始位置触发
    public float speed = 0.05f;                 //移动速度
    public Transform[] theWayPoints;            //移动目标点组
    // Start is called before the first frame update
    void Start()
    {
        
    }
    // Update is called once per frame
    void Update()
    {
        //未达到指定的index位置,调用MoveToThePoints函数每帧继续移动
        if (transform.position != theWayPoints[index].position)
        {
            MoveToThePoints();  
        }
        //到了数组指定index位置,改变index值,不断循环
        else
        {
            index = ++index % theWayPoints.Length;
        }
    }
    void MoveToThePoints()
    {
        //从当前位置按照指定速度移到index位置,记得speed * Time.deltaTime,不然会瞬移
        Vector2 temp = Vector2.MoveTowards(transform.position, theWayPoints[index].position, speed * Time.deltaTime);
        //考虑到可能有碰撞检测,所以使用刚体的移动方式
        GetComponent<Rigidbody2D>().MovePosition(temp);
    
    }
}

将类附在物体上,设置每次移动位置,然后就会按照设置位置移动。

你可能感兴趣的:(按照指定路径移动)