1
 if (Input.GetKeyDown(KeyCode.E))
    {
        Camera.main.transform.eulerAngles = new Vector3(0, 180, 0);
    }

This code should rotate the camera so it is facing behind the player. It is meant to be a "look back" feature of sorts. The problem is, is that it doesn't. It just freaks out and snaps back to its original orientation. Why is this?

2
  • Which event is firing this code? Commented Aug 24, 2018 at 21:53
  • This code is being fired in FixedUpdate @Josh Part Commented Aug 24, 2018 at 21:56

1 Answer 1

2

You are not rotating the GameObject when you press the 'E' key. You're instead setting the rotation of the camera to the-same 180 value when 'E' key is pressed. It will always be 180 each time the key is pressed.

If you want to rotate the camera 180 deg each time the 'E' key is pressed, you have to increment the camera rotation with += instead of just = which simply assigns the angle with 180 deg over and over again:

void Update()
{
    if (Input.GetKeyDown(KeyCode.E))
    {
        Camera.main.transform.eulerAngles += new Vector3(0, 180, 0);
    }
}

You can also use transform.Rotate:

void Update()
{
    if (Input.GetKeyDown(KeyCode.E))
    {
        Camera.main.transform.Rotate(new Vector3(0, 180, 0));
    }
}

Notice how I used the Update function became FixedUpdate is used to add physics force to Rigidbody Objects.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the tip on Update! I thought using FixedUpdate was considered better practice, not sure where i got that from. Thank you! Now for some reason, that code still does not seem to be working for me. It still just freaks out instead of turning around and staying there.
1.Make sure that no other component/script is attached to the camera. 2.Make sure that this script is not attached to multiple objects other. If you have done both the issue is still happening, zip the project, upload it somewhere and provide link to it here. When I have time, I will take a look at it
It was another script! For some reason my first person character controller is messing with it! Thank you!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.