unity3d - Unity key input control statement incorrect -


this script use movement of endless runner game made in unity3d 5.1.2f1 personal. moving right works completely. moving left doesn't work, debug.log work. in inspector can see 'leftrightspeed' set 2 when moving right nothing happens float when moving left. doing wrong here?

(float speed set 5 in inspector).

using unityengine; using system.collections;  public class playercontroller : monobehaviour {      public float speed;     public float leftrightspeed;     private rigidbody rb;      void start()     {     rb = getcomponent<rigidbody>();     }      void fixedupdate()     {         //left         if (input.getkey("left"))         {             leftrightspeed = -2f;             debug.log ("left");         }         else         {             leftrightspeed = 0f;         }           //right         if (input.getkey("right"))         {             leftrightspeed = 2f;             debug.log ("right");         }         else         {             leftrightspeed = 0f;         }      vector3 movement = new vector3 (-2f, 0.0f, leftrightspeed);      rb.addforce (movement * speed);     } } 

jerry correct 2nd if statment cancels out left movement. improve fixedupdate method using getaxis so:

void fixedupdate() {     leftrightspeed = input.getaxis("horizontal") * 2;     vector3 movement = new vector3 (-2f, 0.0f, leftrightspeed);     rb.addforce (movement * speed); } 

essentially getaxis give number between -1 , 1 depending on key being pressed in correlation "horizonal" axis. work game controllers, arrow keys, or w/a/s/d movement default.

check out documentation getaxis.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -