unity敌人自动巡逻动画代码
时间: 2023-10-22 11:03:38 浏览: 118
以下是一个简单的Unity敌人自动巡逻动画的代码示例:
```csharp
using UnityEngine;
public class EnemyPatrol : MonoBehaviour
{
public float speed = 2f; // 敌人移动速度
public Transform[] waypoints; // 巡逻点列表
private int currentWaypointIndex = 0; // 当前巡逻点索引
private void Update()
{
// 计算敌人移动方向
Vector3 direction = waypoints[currentWaypointIndex].position - transform.position;
transform.Translate(direction.normalized * speed * Time.deltaTime, Space.World);
// 检查是否到达当前巡逻点
if (Vector3.Distance(transform.position, waypoints[currentWaypointIndex].position) < 0.1f)
{
// 到达当前巡逻点,更新下一个巡逻点
currentWaypointIndex = (currentWaypointIndex + 1) % waypoints.Length;
// 翻转敌人朝向
transform.Rotate(0f, 180f, 0f);
}
}
}
```
这个脚本将敌人沿着指定的巡逻点列表移动,当敌人到达一个巡逻点时,它会翻转朝向并移动到下一个巡逻点。你可以在巡逻点附近添加触发器来检测玩家是否进入了敌人的视野范围。如果玩家进入了视野范围,你可以切换到敌人追逐状态,否则敌人将继续巡逻。
阅读全文
相关推荐


















