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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Why is this simple transform code not working ?

Discussion in 'Scripting' started by Ostar, Jul 2, 2015.

  1. Ostar

    Ostar

    Joined:
    Nov 9, 2012
    Posts:
    5
    I can move the object normally it moves at speed of 1. but when i press Fire3 on joystick or mouse2 it still moves at speed of 1 ? what am i doing wrong?


    Code (JavaScript):
    1. #pragma strict
    2. private var a:float = 0f;
    3. public var b:int = 1;
    4. public var c:int = 1;
    5. public var boost:int = 4;
    6.  
    7. function Update () {
    8.  
    9. if (Input.GetAxis ("Horizontal") > a  && Input.GetButton ("Fire3") == 1)
    10. {
    11. transform.Translate(boost * Time.deltaTime,0,0);
    12.  
    13. }else if (Input.GetAxis ("Horizontal") > a )
    14. {
    15.  
    16. transform.Translate(b * Time.deltaTime,0,0);
    17. }
    18.  
    19.  
    20.  
    21. if (Input.GetAxis ("Horizontal")  <  a  && Input.GetButton ("Fire3") == 1)
    22. {
    23. transform.Translate(-boost  * Time.deltaTime,0,0);
    24.  
    25. }else if (Input.GetAxis ("Horizontal") < a )
    26. {
    27.  
    28. transform.Translate(-b * Time.deltaTime,0,0);
    29. }
    30.  
    31. }
     
  2. Adroytus

    Adroytus

    Joined:
    Apr 1, 2014
    Posts:
    8
    Input.GetButton() returns true only while the button is held down. Are you holding the Fire3 button down or just pressing it?
     
    Ostar likes this.
  3. Ostar

    Ostar

    Joined:
    Nov 9, 2012
    Posts:
    5
    Holding it down
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    it would help if you read the error message that code generates, at least in c# :p...

    Input.GetButton("Fire3") returns a boolean, you can't compare that to an int


    to fix, get rid of the "== 1", it's a boolean already, it doesn't need anything to compare to
     
    Ostar likes this.
  5. Ostar

    Ostar

    Joined:
    Nov 9, 2012
    Posts:
    5
    Holding it down.
     
  6. Ostar

    Ostar

    Joined:
    Nov 9, 2012
    Posts:
    5
    It worked.
    Thank You .