Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

when i turn the camera with

var tspeed:float=2.0;

function Start () {

}

function Update () {
if(Input.GetButtonDown("tleft")){
transform.Rotate(0, tspeed* Time.deltaTime, 0);
}
if(Input.GetButtonDown("tright")){
transform.Rotate(0, -tspeed* Time.deltaTime, 0);
}

}

it works when pressing the button once but when i hold it it dosnt turn any more and i have to press the button again.how to make it turn when i hold the key?

im using javascript.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Use Input.GetButton("tleft") instead.

GetButtonDown will return true in one update, after the user has pressed the key down. It won't return true again until the user releases the key and presses it again.

GetButton, however, will be true in each update as long as the user has the key held down during that frame.

share|improve this answer
    
I highly recommend this option as it eliminates the need for saving your own states. –  Raceimaztion Aug 9 '12 at 6:07

Input.GetButtonDown is fired during the frame when user pressed the button. The function return true only one time when user pressed it.

From the Unity Script Reference: Input.GetButtonDown()

Returns true during the frame the user pressed down the virtual button identified by You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has released the key and pressed it again.

You can do something like this:

var rightButtonDown= false;
var leftButtonDown= false;

function Update(){
    if(rightButtonDown){
        //rotate...
    }

    if(leftButtonDown){
        //rotate...
    }

    if(Input.GetButtonDown("tleft")){
        leftButtonDown= true;
    }
    else if (Input.GetButtonUp("tleft")){
        leftButtonDown= false;
    }

    if(Input.GetButtonDown("tright")){
        rightButtonDown= true;
    }
    else if (Input.GetButtonUp("tright")){
        rightButtonDown= false;
    }
}

EDIT

I've made a huge mistake in my previous explanation.

GetButtonDown() is fired only during one frame when the button is pressed as well for GetButtonUp() when the button is released. So to know if the button is hold or not, you have to check state changes when GetButtonDown() or GetButtonUp() are true.

For example, you have a variable "buttonDown" wich is a bool(true, the button is down / false, the button is up). When GetButtonDown() return true, you can assume that the button is hold by user. So you assign true to "buttonDown" and you only change the value of "buttonDown" when GetButtonUp() return true.

share|improve this answer
    
thanks but i cant get the code you posted to work,it dosnt turn when i press the button. –  robotmaker01 Aug 8 '12 at 12:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.