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.

First off, I'm sorry this is such a silly question, but for the life of me I can't seem to get it to work. And I've just spent the last hour trying to do so.

All I want to do is have my camera move up, in a smooth fashion, when I press the shoulder button on my joypad. I have it so that when I press the button, it jumps up. Not in a smooth movement.

The code I have is as follows:

    float _maxY = 0.0f;          
    if (Input.GetKeyDown(KeyCode.Joystick1Button5))
   {
      _maxY++;
      transform.Translate(0, _maxY, 0);
   }

This is found within my update function. I've been looking for the answer to this for hours now, but can't find anything. Can anyone please help me?

share|improve this question
add comment

3 Answers

Read the docs - the GetKeyDown function triggers exactly once, in the frame when the button is pressed. That code will therefore only be executed once per press. If you want to move continuously, you need to use one of the functions that continuously detects that a key is pressed in that frame, and move a small amount each time.

share|improve this answer
add comment

You shouldn't increment from 0 every frame, you should increment from the object's current position.

share|improve this answer
add comment

The easiest solution is to make the movement amount smaller, something like _maxY+=.05f;

Additionally, you'll want to modify your code to react to the GetKeyDown function. In the update loop you'd have something like:

if (Input.GetKeyDown(KeyCode.Joystick1Button5))
{
     goUp = true;
}

if (Input.GetKeyUp(KeyCode.Joystick1Button5))
{
     goUp = false;
}

if(goUp){
     _maxY+=.05f;
     transform.Translate(0, _maxY, 0);
}
share|improve this answer
add comment

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.