我该怎么做才能让我的敌人在目标跳跃时不会改变它的高度?
How to I make it so my enemy doesn't change it's height when the target jumps?
我一直在为游戏中的敌人移动编写脚本,但是当目标(玩家)跳跃时,敌人开始逐渐漂浮到目标所在的相同 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);
}
}
移动前设置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);
}
}
我一直在为游戏中的敌人移动编写脚本,但是当目标(玩家)跳跃时,敌人开始逐渐漂浮到目标所在的相同 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);
}
}
移动前设置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);
}
}