2017-10-02 2 views
0

Das ist mein Skript für die Spielerbewegung in Unity (2d).Wie man diagonale Bewegung stoppt - Einheit 2d?

Wenn zwei Richtungstasten gedrückt werden - statt diagonal zu bewegen - ich brauche den Spieler in der zuletzt gedrückten Richtung zu bewegen (und wenn das freigegeben wird, die Richtung der bereits nach unten gehalten)

if (!attacking) 
{ 
    if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f) 
    { 
     //transform.Translate (new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f)); 
     myRigidBody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * currentMoveSpeed, myRigidBody.velocity.y); 
     PlayerMoving = true; 
     lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f); 
    } 

    if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f) 
    { 
     //transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f)); 
     myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, Input.GetAxisRaw("Vertical") * currentMoveSpeed); 
     PlayerMoving = true; 
     lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical")); 
    } 
} 
+1

ein paar private Variablen hinzufügen, den vorherigen Wert erinnern und vergleichen? Suchen Sie nach grundlegenden Ideen, wie Sie damit umgehen sollen, oder haben Sie Probleme bei der Codierung? –

Antwort

2

Hier ist, wie ich damit umgehen würde: Wenn nur eine Achse aktiv ist (horizontal oder vertikal), erinnere dich an diese Richtung. Wenn beides der Fall ist, priorisiere denjenigen, der es nicht war. Der folgende Code funktioniert genau so, wie Sie es beschrieben haben, muss aber an Ihre anderen Anforderungen angepasst werden.

void Update() 
{ 
    float currentMoveSpeed = moveSpeed * Time.deltaTime; 

    float horizontal = Input.GetAxisRaw("Horizontal"); 
    bool isMovingHorizontal = Mathf.Abs(horizontal) > 0.5f; 

    float vertical = Input.GetAxisRaw("Vertical"); 
    bool isMovingVertical = Mathf.Abs(vertical) > 0.5f; 

    PlayerMoving = true; 

    if (isMovingVertical && isMovingHorizontal) 
    { 
     //moving in both directions, prioritize later 
     if (wasMovingVertical) 
     { 
      myRigidBody.velocity = new Vector2(horizontal * currentMoveSpeed, 0); 
      lastMove = new Vector2(horizontal, 0f); 
     } 
     else 
     { 
      myRigidBody.velocity = new Vector2(0, vertical * currentMoveSpeed); 
      lastMove = new Vector2(0f, vertical); 
     } 
    } 
    else if (isMovingHorizontal) 
    { 
     myRigidBody.velocity = new Vector2(horizontal * currentMoveSpeed, 0); 
     wasMovingVertical = false; 
     lastMove = new Vector2(horizontal, 0f); 
    } 
    else if (isMovingVertical) 
    { 
     myRigidBody.velocity = new Vector2(0, vertical * currentMoveSpeed); 
     wasMovingVertical = true; 
     lastMove = new Vector2(0f, vertical); 
    } 
    else 
    { 
     PlayerMoving = false; 
     myRigidBody.velocity = Vector2.zero; 
    } 
} 

Beispielergebnis (rosa Linie ist lastMove): Result