【问题标题】:How to I make it so my enemy doesn't change it's height when the target jumps?我怎么做才能让我的敌人在目标跳跃时不会改变它的高度?
【发布时间】: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);
     }
 }

【问题讨论】:

    标签: c# unity3d unity5


    【解决方案1】:

    在移动之前设置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);
        }
    }
    

    【讨论】:

    • goto 是 C# 关键字。不要将其用作变量名。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多