如何让它成为我的敌人在目标跳跃时不会改变它的高度?

时间:2017-06-12 21:37:47

标签: c# unity3d unity5

我一直在为我的游戏中的敌人运动编写一个脚本,但是当目标(玩家)跳跃时,敌人开始逐渐浮动到目标所在的相同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);
     }
 }

1 个答案:

答案 0 :(得分:2)

在进行移动之前只需设置y

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);
        Vector3 target = Player.position;
        target.y = transform.position.y;
        //finding the distance between the enemy and the player
        float distance = Vector3.Distance(transform.position, target);
        if(distance < walkingDistance){
        //moving the enemy towards the player
        transform.position = Vector3.SmoothDamp(transform.position, 
        target, ref Velocity, speed);
    }
}