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. Dismiss Notice

Change WasD into Horizontal and Vertical for mobile controls

Discussion in 'Scripting' started by jorn818, Mar 11, 2016.

  1. jorn818

    jorn818

    Joined:
    May 12, 2013
    Posts:
    97
    Working on a 2d days styled survival game but my animation script needs to be changed into horizontal for the D-pad to work :/
    [script]
    using CnControls;
    function Update(){
    if(Input.GetKey("w")){
    GetComponent.<Animation>().Play("upwalk");
    }
    if(Input.GetKey("s")){
    GetComponent.<Animation>().Play("downwalk");
    }
    if(Input.GetKey("d")){
    GetComponent.<Animation>().Play("rightwalk");
    }
    if(Input.GetKey("a")){
    GetComponent.<Animation>().Play("leftwalk");
    }
    }

    [script]

    this is my script but how can I change this in positive or negative horizontal or vertical, can someone make an example for just one (so for example just W) so that I can try it myself on the rest?
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Input.GetAxisRaw("Horizontal") returns -1 to 1 you can use that in your if statement
     
  3. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    quick clarification
    GetAxis() returns a float from -1 to 1.
    GetAxisRaw() returns only -1, 0, or 1.
     
    jorn818 and Polymorphik like this.
  4. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Yeah so Unity has its own smoothing function to mimic an axis in a way. So when you use the WASD keys it will gradual move from 0.0F to -1.0F/1.0F over time. This is from Input.GetAxis(string:name). Input.GetAxisRaw(string:name) does not smooth anything it always returns -1.0F, 0.0F or 1.0F.
     
  5. jorn818

    jorn818

    Joined:
    May 12, 2013
    Posts:
    97
    How do I know if it is positive or negative, or how does the script know
     
  6. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    put it in an if statement?

    Code (CSharp):
    1. float horizontal = Input.GetAxisRaw("Horizontal");
    2.  
    3. if(horizontal < 0.0F) {
    4. // Left
    5. } else if(horizontal > 0.0F) {
    6. // Right
    7. } else {
    8. // Nothing on the horizontal
    9. }
    repeat for vertical.
     
  7. jorn818

    jorn818

    Joined:
    May 12, 2013
    Posts:
    97
    Code (csharp):
    1.  
    2.  
    3. Function Update() {
    4. float horizontal = Input.GetAxisRaw("Horizontal");
    5.  }
    6. if(horizontal < 0.0F) {
    7.   GetComponent.<Animation>().Play("leftwalk");
    8. // Left
    9. } else if(horizontal > 0.0F) {
    10.  GetComponent.<Animation>().Play("rightwalk");
    11. // Right
    12. } else {
    13. // Nothing on the horizontal
    14. {
    15. float vertical = Input.GetAxisRaw("Vertical");
    16. }
    17. if(vertical < 0.0F) {
    18.   GetComponent.<Animation>().Play("upwalk");
    19. // Left
    20. } else if(vertical > 0.0F) {
    21.  GetComponent.<Animation>().Play("downwalk");
    22. // Right
    23. } else {
    24. // Nothing on the horizontal
    25. }
    26.  
    27. }
    28.  
    29. [code]
    30.  
    31. apparently I need to add a semicolon to both floats which are already there ;( maybe I F***ed up the brackeys somehow? I feel like such a newb lol xD