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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

2d arrow key controls are not working :(

Discussion in '2D' started by downrightgames, Sep 25, 2017.

  1. downrightgames

    downrightgames

    Joined:
    Sep 25, 2017
    Posts:
    9
    so I following this unity tutorial however the controls don't work for some reason :(
    Everything else works (animation/sprites appear)

    Tutorial link:


    P.S I also tried re-creating the script and replying it to the sprite several times :/
     
  2. Oddinx

    Oddinx

    Joined:
    Mar 2, 2017
    Posts:
    12
    Maybe the player speed is in 0 or maybe you typed horizontall in lowercase. You should show your code and the inspector window of your character.
     
    downrightgames likes this.
  3. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    Please show your code using the insert feature of the forum.
    You may also check your inputs into project settings.
     
    downrightgames likes this.
  4. downrightgames

    downrightgames

    Joined:
    Sep 25, 2017
    Posts:
    9
    so here is the code. I got also which horizontal was supposed to be capitalized? the first or second? float horizontal = Input.GetAxis ("Horizontal");



    using UnityEngine;

    using System.Collections;
    public class Player : MonoBehaviour {
    public float speed = 50f;
    public float jumpPower = 150f;
    public bool grounded;

    private Rigidbody2D rigidBody2D;

    void Start ()
    {
    rigidBody2D = gameObject.GetComponent<Rigidbody2D>();
    }
    void Update ()

    {



    }



    void FixedUpdate ()
    {

    float horizontal = Input.GetAxis ("Horizontal");

    rigidBody2D.AddForce ((Vector2.right * speed) * horizontal);
    Debug.Log(horizontal);


    }

    }



    Also when I set my character speed to 5 it just runs in place :/

    I also tried using Debug.Log(horizontal); when I pressed the arrow keys the numbers changed in the console so idk whats wrong :/
     
    Last edited: Sep 25, 2017
  5. downrightgames

    downrightgames

    Joined:
    Sep 25, 2017
    Posts:
    9
    this is my inspector for my player Screen Shot 2017-09-25 at 11.18.36 AM.png Screen Shot 2017-09-25 at 11.18.49 AM.png Screen Shot 2017-09-25 at 11.18.49 AM.png
     
  6. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    Open your Player.cs script, copy the code inside it and insert it (clicking on the "News-paper" like button and choosing c#) on the forum.
     
  7. downrightgames

    downrightgames

    Joined:
    Sep 25, 2017
    Posts:
    9
    Code (C#):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4.  
    5.  
    6.  
    7. public class Player : MonoBehaviour {
    8.  
    9.  
    10.  
    11.     public float Speed = 50f;
    12.  
    13.     public float jumpPower = 150f;
    14.  
    15.     public bool ground;
    16.  
    17.     private Rigidbody2D rigidBody2D;
    18.  
    19.  
    20.  
    21.     void Start ()
    22.  
    23.     {
    24.  
    25.         rigidBody2D = gameObject.GetComponent<Rigidbody2D>();
    26.  
    27.     }
    28.  
    29.  
    30.  
    31.     void Update ()
    32.  
    33.     {
    34.  
    35.  
    36.  
    37.     }
    38.  
    39.  
    40.  
    41.     void FixedUpdate ()
    42.     {
    43.  
    44.         float horizontal = Input.GetAxis ("Horizontal");
    45.  
    46.         rigidBody2D.AddForce ((Vector2.right * Speed) * horizontal);
    47.         Debug.Log(horizontal);
    48.  
    49.  
    50.     }
    51.  
    52. }
     
  8. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    You said "when you set your speed to 5 it runs in place", even with a speed of 50, it doesn't move?

    if you replace: rigidBody2D.AddForce ((Vector2.right * Speed) * horizontal);
    by : rigidBody2D.velocity = transform.right * Speed * horizontal ;
    Does something happen?
    (actually, i don't understand why it doesn't work, i added the script in a new project and the player moves, it accelerates)
     
    Last edited: Sep 27, 2017
  9. downrightgames

    downrightgames

    Joined:
    Sep 25, 2017
    Posts:
    9
    I added replaced line of code however it still did not work. :( if It helps you figure out what is wrong the falling animation also does not trigger when the player is in mid air also the Ground on the players inspector thing has a capital G in ground however the ground in the animator has a lower case g in ground. the Capital ground in the inspector menu does nothing when I toggle it. while the one in the animator determines when the player is in mid air. What is even more interesting is that when I change the boolean Ground to ground in the script It does not change in the inspector file.........
     
  10. downrightgames

    downrightgames

    Joined:
    Sep 25, 2017
    Posts:
    9
    When I tried changing the ground to Ground in the animator window nothing changed other than the variable name :/
     
  11. downrightgames

    downrightgames

    Joined:
    Sep 25, 2017
    Posts:
    9
    Its as if Ground in the script and Ground in the animator are different variables and I have no clue how to fix this.
     
  12. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    Nothing in your player code modifies the animator bool. About that, i guess you just need to add
    at the beginning of your code: public Animator anim;
    inside of Start() : anim = GetComponent<Animator>();
    inside of FixedUpdate() : anim.SetBool("ground", ground);

    I believe you made a mistake somewhere else, and i can't tell where, as your code works flawless in a new project i created specially to test your code. Maybe you should find another beginner tutorial looking like this one, and start a brand new project.
     
  13. downrightgames

    downrightgames

    Joined:
    Sep 25, 2017
    Posts:
    9
    mhh now the ground float works in sync, however the player still does not move and the ground variable does not change depending on if the player is on the ground or not.
     
  14. downrightgames

    downrightgames

    Joined:
    Sep 25, 2017
    Posts:
    9
    OMG I tried adding an rgb body thing to the ground and now It falls 0_0
     
  15. ExDeaDguY

    ExDeaDguY

    Joined:
    Aug 25, 2009
    Posts:
    503
    I'm not too familiar with this tutorial but I would suggest:

    This is a 2d game? You should maybe go into the physics settings and make sure the gravity is in the proper direction. So y would be -10. If you started a new project and did not select 2d from the start, the gravity by default ends up with z -9.something.

    Also, Check your project inputs. Make sure the arrow keys are in fact listed with the name of "Horizontal".