Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

player walking up and right at the same time topdown 2dgame

Discussion in 'Scripting' started by Vexer, May 14, 2018.

  1. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Hey guys i would like to prevent my player from doing this:
    https://gyazo.com/e5f7608e379a259812c0cc8328073dba
    i only want him to be able to move up,down,right or left but not all at the same time.. please let me know what i am doing wrong:

    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class PlayerController : MonoBehaviour
    8. {
    9.     public float speed;
    10.  
    11.     public bool up;
    12.     public bool down;
    13.     public bool left;
    14.     public bool right;
    15.  
    16.     private Animator anim;
    17.     private Rigidbody2D rb;
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Start()
    22.     {
    23.         speed = 2;
    24.  
    25.         anim = gameObject.GetComponent<Animator>();
    26.         rb = gameObject.GetComponent<Rigidbody2D>();
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         anim.SetBool("up", up);
    32.  
    33.         anim.SetBool("down", down);
    34.  
    35.         anim.SetBool("left", left);
    36.  
    37.         anim.SetBool("right", right);
    38.     }
    39.  
    40.     void FixedUpdate()
    41.     {
    42.         float horizontal = Input.GetAxisRaw("UpAndDown");
    43.         float vertical = Input.GetAxisRaw("LeftAndRight");
    44.  
    45.         HandleMovementhorizontal(horizontal);
    46.         HandleMovementvertical(vertical);
    47.  
    48.     }
    49.  
    50.     private void HandleMovementhorizontal(float horizontal)
    51.     {
    52.         rb.velocity = new Vector2(horizontal * speed, rb.velocity.x);
    53.  
    54.         if (Input.GetKey(KeyCode.D))
    55.         {
    56.             right = true;
    57.  
    58.             anim.SetBool("right", true);
    59.         }
    60.  
    61.         if (!Input.GetKey(KeyCode.D))
    62.         {
    63.             right = false;
    64.  
    65.             anim.SetBool("right", false);
    66.         }
    67.  
    68.         if (Input.GetKey(KeyCode.A))
    69.         {
    70.             left = true;
    71.  
    72.             anim.SetBool("left", true);
    73.         }
    74.  
    75.         if (!Input.GetKey(KeyCode.A))
    76.         {
    77.             left = false;
    78.  
    79.             anim.SetBool("left", false);
    80.         }
    81.     }
    82.  
    83.     private void HandleMovementvertical(float vertical)
    84.     {
    85.         rb.velocity = new Vector2(vertical * speed, rb.velocity.x);
    86.  
    87.         if (Input.GetKey(KeyCode.W))
    88.         {
    89.             up = true;
    90.  
    91.             anim.SetBool("up", true);
    92.         }
    93.  
    94.         if (!Input.GetKey(KeyCode.W))
    95.         {
    96.             up = false;
    97.  
    98.             anim.SetBool("up", false);
    99.         }
    100.  
    101.         if (Input.GetKey(KeyCode.S))
    102.         {
    103.             down = true;
    104.  
    105.             anim.SetBool("down", true);
    106.         }
    107.  
    108.         if (!Input.GetKey(KeyCode.S))
    109.         {
    110.             down = false;
    111.  
    112.             anim.SetBool("down", false);
    113.         }
    114.     }
    115. }
    116.  
    117.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you might want to use triggers instead of bools in the animator so the "if not" case is handled automatically; but the main reason is you're using "if... if... if..." instead of "if... else if...." and splitting the movement control into two functions when the effect you want depends on both inputs.
     
  3. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Thx for your answer it fixed my game a little bit the animations are not glitching anymore but my player can still walk up and to the right at the same moment:

    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class PlayerController : MonoBehaviour
    8. {
    9.     public float speed;
    10.  
    11.     public bool up;
    12.     public bool down;
    13.     public bool left;
    14.     public bool right;
    15.  
    16.     private Animator anim;
    17.     private Rigidbody2D rb;
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Start()
    22.     {
    23.         speed = 2;
    24.  
    25.         anim = gameObject.GetComponent<Animator>();
    26.         rb = gameObject.GetComponent<Rigidbody2D>();
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.     }
    32.  
    33.     void FixedUpdate()
    34.     {
    35.         float horizontal = Input.GetAxisRaw("LeftAndRight");
    36.         float vertical = Input.GetAxisRaw("UpAndDown");
    37.  
    38.         HandleMovement(horizontal, vertical);
    39.  
    40.     }
    41.  
    42.     private void HandleMovement(float horizontal, float vertical)
    43.     {
    44.         rb.velocity = new Vector3(horizontal,vertical * speed, rb.velocity.x);
    45.  
    46.         if (Input.GetKey(KeyCode.D))
    47.         {
    48.             right = true;
    49.  
    50.             anim.SetTrigger("right");
    51.         }
    52.  
    53.        else if (Input.GetKey(KeyCode.A))
    54.         {
    55.             left = true;
    56.  
    57.             anim.SetTrigger("left");
    58.         }
    59.  
    60.        else if (Input.GetKey(KeyCode.W))
    61.         {
    62.             up = true;
    63.  
    64.             anim.SetTrigger("up");
    65.         }
    66.  
    67.         else if (Input.GetKey(KeyCode.S))
    68.         {
    69.             down = true;
    70.  
    71.             anim.SetTrigger("down");
    72.         }
    73.  
    74.         else
    75.         {
    76.             anim.Play("Idle");
    77.         }
    78.     }
    79.  
    80. }
    81.  
    82.  
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    your current design:
    Code (csharp):
    1.  
    2. HandleMovement()
    3. {
    4.     move based on axis input
    5.    
    6.     if key input set animation
    7.         ...
    8.         ...
    9.         ...
    10. }
    11.  
    should look something more like
    Code (csharp):
    1.  
    2. HandleMovement()
    3. {
    4.     declare vector
    5.     if key input (or from axis)
    6.         set animation, set vector component
    7.         ...
    8.         ...
    9.         ...
    10.        
    11.     move based on built vector
    12. }
    13.  
     
  5. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Could you give me an example line of code i don't really seem to get it?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    A couple of things come to mind. 1 you didn't ask about, but this is at least the second time I've seen someone doing something similar recently.
    You have the horizontal and vertical axis input, already, but then you proceed to check the keys, which are for the same purpose. You can skip that by using the values you have.

    Onto your actual question, choose which way you want to win in a tie, perhaps and write something like:
    Code (csharp):
    1. if(horizontal != 0) {
    2.    // moving left/right
    3.   }
    4. else if (vertical != 0){
    5.    // moving up or down, but not right or left
    6.  }
    That requires a small bit of adjustment if you want to prevent changing direction, if the key is already pressed, eg: if already moving vertical, but the horizontal axis now has a non-zero value, would you continue vertical or switch to horizontal.. :)