I'm rather new to C#, and i'm trying to make the camera turn

My code is this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LookBack : MonoBehaviour
{
    public Transform cam;
    public int rotation;
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("e"))
        {
            // here is wrong 
            transform.localRotation = Quaternion.Euler(0f, 0f, rotation);
            UnityEngine.Debug.Log("E is being pressed");
        }
    }
}

I know from the debug log that it does get the input, which means that something is wrong in the “// here is wrong” part, and i have also provided an image with the interface with the hierarchy etc.

If anyone could help it would really be appreciated :wink:

What does it do? From my reading it should invert the camera since you are changing the Z value of the local rotation.

However, if MouseLook is driving it back to regular orientation every frame, then you’d probably see nothing except perhaps a flicker. Is MouseLook driving the rotation too?

Yeah, it’s supposed to turn the camera back since i’m trying to make a system like the one in Baldi’s Basics when you press space, but it appears to do nothing.

EDIT:: MouseLook is turning the camera, but using the mouse so i’m trying to make it so that it still works but inverted (for the Z axis)

This is because mouselook is …

One approach is to parent two gameObjects to each other, one for the looking, one for the flipping around, otherwise you would have to combine the output of these scripts in code to arrive at a rotation.

Can you please explain how to do that cause i am still learning how unity works :eyes:

I believe Kurt is saying you keep the mouse look script attached to the camera GameObject. You make the camera GameObject a child of a new GameObject where you try attaching the LookBack script. You also make sure your mouse look script applies local rotation instead of world space.

You do that and the mouse look still moves the camera around, by moving the child object, and then LookBack script can flip both the parent and child object around, without affecting the mouse look script.

1 Like

Thank you so much!