Search Unity

Beginner Question on Inputs

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

  1. BobbyDoogle


    Dec 3, 2013
    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


    Dec 29, 2011

    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


    Jun 20, 2013
    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);