【发布时间】:2017-06-13 05:58:41
【问题描述】:
我一直在为我的游戏中的敌人移动编写一个脚本,但是当目标(玩家)跳跃时,敌人开始逐渐漂浮到目标所在的相同 y 位置。如果敌人留下,我会喜欢在与地面相同的位置,但我还没有发现我将如何做到这一点。我是 Unity 的新手,所以我唯一能想到的就是为敌人添加一个刚体,但这似乎不起作用。有人对如何做到这一点有任何想法吗?这是我的脚本:
public class EnemyMovement : MonoBehaviour {
//target
public Transform Player;
//the distace the enemy will begin walking towards the player
public float walkingDistance = 10.0f;
//the speed it will take the enemy to move
public float speed = 10.0f;
private Vector3 Velocity = Vector3.zero;
void Start(){
}
void Update(){
transform.LookAt (Player);
//finding the distance between the enemy and the player
float distance = Vector3.Distance(transform.position, Player.position);
if(distance < walkingDistance){
//moving the enemy towards the player
transform.position = Vector3.SmoothDamp(transform.position,
Player.position, ref Velocity, speed);
}
}
【问题讨论】: