1
\$\begingroup\$

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?

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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