2017-06-12 1 views
0

Ich habe in meinem Spiel ein Skript für feindliche Bewegungen geschrieben, aber wenn das Ziel (Spieler) springt, beginnt der Feind allmählich auf die gleiche y-Position zu schweben, in der sich das Ziel befand. Ich würde es gerne sehen, wenn der Feind dort bleiben würde die gleiche Position wie der Boden, aber ich habe nicht herausgefunden, wie ich das schaffen könnte. Ich bin neu in der Einheit, also war das einzige, was ich mir vorstellen konnte, dem Feind einen starren Körper hinzuzufügen, aber das schien nicht zu funktionieren. Hätte jemand eine Idee, wie man das macht? Hier ist mein Skript:Wie kann ich es so machen, dass mein Gegner seine Höhe nicht ändert, wenn das Ziel springt?

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); 
    } 
} 

Antwort

2

Nur stellen Sie den y Wert vor der Bewegung zu tun

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); 
    } 
} 
+1

'goto' ist ein C# Schlüsselwort. Verwenden Sie es nicht als Variablennamen. – Programmer

+0

danke, das habe ich nicht bemerkt – maksymiuk

Verwandte Themen