Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Beginner Question on Inputs

Discussion in 'Scripting' started by BobbyDoogle, Dec 3, 2013.

  1. BobbyDoogle

    BobbyDoogle

    Joined:
    Dec 3, 2013
    Posts:
    15
    I'm new to Unity, coming from GML (Game Maker),

    I am creating physics motion as such:

    void Update () {
    if(touchingPlatform Input.GetButtonDown("Vertical")){
    rigidbody.AddForce(verticalVelocity, ForceMode.VelocityChange);
    }
    }

    This works great, and when I hit W the player moves "North"
    Two questions
    1. How would I code in the opposite (or negative) direction, project/inputs only provides a vertical and "negative", I don't see 4 directions in a given plane. Is there some way to simply apply the reverse?
    2. This moves the player a declared "verticalVelocity" each time the button is pressed, how could I make it apply a constant amount while the button is held down.

    I'm sure I'll look back at this post and be amazed at how little I knew now : ' /

    I thought I would post this to get started on the forum.
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    12,655
    Welcome!

    1. You'll see that there's "Vertical" and "Horizontal" in the input settings, and each has either a positive and negative button or a joystick axis (or both). When you use Input.GetAxis("blah") it will return you a value between 1.0 and -1.0 depending on what's being pressed for that axis, rather than just a boolean. Between those two things you can get all 4 directions.

    2. Actually, that add's a force to the player's Rigidbody component. If you want the velocity to be constant then modify its "velocity" attribute instead.

    Also, it's easier to read your code (and help you;)) if you use code tags.
     
  3. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,497
    You could use
    Code (csharp):
    1.  Input.GetKey(KeyCode.S);
    for a negative Vertical input.

    Or you could use
    Code (csharp):
    1.  if(Input.GetAxis("Vertical")<0);
     
unityunity