游戏中,怪物会自动的往玩家所在地点走去,那需要创建一个C#脚本EnemyAI,包含两个功能:
1. 怪物旋转自己对准玩家
2. 怪物向前移动,追逐玩家
public class EnemyAI : MonoBehaviour { public Transform target; public int moveSpeed; public int rotateSpeed; private Transform myTransform; void Awake(){ myTransform = transform; } // Use this for initialization void Start () { GameObject go = GameObject.FindGameObjectWithTag("Player"); target = go.transform; } // Update is called once per frame void Update () { Debug.DrawLine (target.position, transform.position, Color.yellow); //使敌人始终旋转自己对着Player myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation (target.position - myTransform.position), rotateSpeed * Time.deltaTime); //怪物向着Player的方向移动 myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; } }
将之绑定到cube上,修改moveSpeed和rotateSpeed为1,cube始终look着player对象。
运行,然后调整RotationSpeed=5,moveSpped=3,使运动更加真实。
毫无理由的 坚持、坚持、再坚持!