How do I make the camera move up? (Javascript)

I want to make the camera move up slightly when ‘r’ is pressed. I’ve made some code but for some reason it’s making the camera fly up really quickly.

function Start () {

}

function Update () {

if(Input.GetKeyDown (KeyCode.R));
	{
		transform.position = Vector3((transform.position.x),((transform.position.y+10)),(transform.position.z));
		}
}

Note the ';' at the end of line 7 should be removed.

1 Answer

1

Your code is moving the camera up 10 units per frame. Try:

private var speed = 2.0;

function Update () {
 
    if(Input.GetKeyDown (KeyCode.R))
    {
       transform.position = Vector3((transform.position.x),((transform.position.y+Time.deltaTime * speed)),(transform.position.z));
    }
}

Or:

private var speed = 2.0;

function Update() {
    if(Input.GetKeyDown (KeyCode.R))
        transform.Translate(0.0, speed * Time.deltaTime, 0.0);
}

This will move things up at the rate of 2 units per second.

I just realised I wasn't too clear, I'd like the camera to suddenly move up Y by 10. I thought that by adding 10 to the Y value it would increase it by 10.

As mentioned in my comment, the problem is the ';' at the end of line 7. Remove it, and your code should work as you indented.

Thanks so much!

Remember to accept robertbu's answer if it solved the problem