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

change arrow keys to move and jump help

Discussion in 'Scripting' started by SuperCrow2, Nov 18, 2018.

  1. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    The following code gets him to move left and right but it uses the "up and down" arrow keys. How do I change it so it uses the "right and left" arrow keys instead? How do I make him jump?

    Code (CSharp):
    1. public float spead;
    2. private Rigidbody2D rb;
    3.  
    4. void Start()
    5.     {
    6.         rb = GetComponent<Rigidbody2D>();
    7.     }
    8.  
    9.     private void FixedUpdate()
    10.     {
    11.         float h = Input.GetAxis("Horizontal") * 5;
    12.         float v = Input.GetAxis("Vertical") * 5;
    13.  
    14.         Vector3 vel = rb.velocity;
    15.         vel.x = h;
    16.         vel.x = v;
    17.         rb.velocity = vel;
    18.  
    19.      
    20.  
    21.     }
    22.  
    23. }
    24.  
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Code (CSharp):
    1. vel.x = h;
    2. vel.x = v;
    Something here is wrong. See if you can spot it
     
  3. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    I did it a different way but having more than one way of doing it is nice
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Perhaps you missed my point
    Code (CSharp):
    1.     vel.x =
    2.     vel.x =
     
  5. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,459
    You must modify the default input in Edit/Project Settings/Input: https://docs.unity3d.com/Manual/class-InputManager.html