C# - How do I stop the player from looking behind?

Hi, I’m currently working on a horror game where you’re stuck in a vent shaft and I’m hoping to make it so you cant look behind you when your in the vent, however, I can’t find any way to code this into the game, I’v even tried doing it without coding but with objects in the game. The code I have now:

    void Update ()
    {
        float rotation = transform.eulerAngles.y;

        transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed * Time.deltaTime, 0);
        currentMovement = new Vector3 (0, currentMovement.y, Input.GetAxis ("Vertical") * moveSpeed);
        currentMovement = transform.rotation * currentMovement;

        if (rotation > 90 && rotation < 180)
            transform.Rotate (0, -5, 0);
        if (rotation < 270 && rotation > 180)
            transform.Rotate (0, 5, 0);

        if (!controller.isGrounded)
            currentMovement -= new Vector3(0, gravity * Time.deltaTime, 0);
        else
            currentMovement.y = -0.5f;

        if (controller.isGrounded && Input.GetButtonDown("Jump"))
            currentMovement.y = jumpSpeed;

        controller.Move (currentMovement * Time.deltaTime);
    }

(I know it’s not the best code or the most compact but I’m not looking for people to correct my code unless it’s necessary).

It’s hard to try and explain why I can’t use this code but I’ll try my best - Say if the player is walking forward, the code works fine, he can’t turn around to look behind, but say if they go in to a vent on the left then they can’t turn to face the left wall as that is the limit of rotation, but what they can do is look behind them on the right, finally, if they then want to go in another vent on the left they won’t be able to.

I understand if you don’t understand what I’m trying to explain however if you do I would appreciate the help that I can get, thanks!

The reason your code behaves the way it does is because you’re using the world rotation value instead of the local one: transform.eulerAngles instead of transform.localEulerAngles.

Now I know you said you weren’t looking for a correction in code, but I just want to make a note and say that this is not a good way of approaching rotation constraints. You are counter rotating when the rotation goes above a threshold - this will undoubtedly result in very jerky movement if the player quickly looks left or right. Instead, in your camera control script, you should not allow the camera to rotate more once the limit has been reached, even if the player is moving the mouse further in that direction.

I give you an Example of mine code :wink: you can take it if y want :))
I hope it help y :wink:

And one more if you have no Joystick remove the Joystick Part:
Remove that:
rot_euler.x += ReturnMaxProduct(Input.GetAxis(“Mouse Y”) * MouseFactor * Moving.MouseSensitive, Input.GetAxis(“Y_Joystick”) * Moving.MouseSensitive);
Insert that on the Same Place
rot_euler.x += Input.GetAxis(“Mouse Y”) * MouseFactor * Moving.MouseSensitive;

The hole code:

using UnityEngine;
using System.Collections;

public class LookUpDown : MonoBehaviour
{

    public static bool MouseInherted = false;
    // Begrenzungen
    public float minX = -45;
    public float maxX = 45;

    private Vector3 rot_euler = new Vector3(0,0,0); // Rotation ofx in degree
    public PlayerUseObject playerUseObject = null;

    void Start()
    {
        rot_euler = transform.localEulerAngles;
    }

    // if you have no joystick remove the Joystick part ;)
    void Update()
    {
        // Switch mouse direction if necessary
        float MouseInhertedVal = 1.0f;
        if (MouseInherted) MouseInhertedVal = -1.0f;

            float MouseFactor = 1;
            if (!MouseInherted) MouseFactor = -1;
            // If no Joystick: Remove and Insert the code from the top, at the top ;)
            rot_euler.x += ReturnMaxProduct(Input.GetAxis("Mouse Y") * MouseFactor * Moving.MouseSensitive, Input.GetAxis("Y_Joystick") * Moving.MouseSensitive);

        //  Limitate the up down look
        rot_euler.x = Mathf.Clamp(rot_euler.x, minX, maxX);
        // And set the rotation, puh now it is done ;)
        transform.localEulerAngles = rot_euler;
    }

    public static float ReturnMaxProduct(float pVariable1, float pVariable2)
    {
        float lower = Mathf.Min(pVariable1, pVariable2);
        float higher = Mathf.Max(pVariable1, pVariable2);
        if (lower == 0) return higher;
        if (higher == 0) return lower;

        if (higher > lower * -1)
        {
            return higher;
        }
        else
        {
            return lower;
        }
    }
}

P.S. I search somebody for my Projekt :wink:

I appreciate all the help however I still run into the same problem, the restrictions of the rotation are always the same, no matter which way I am facing/moving. For example, the player is walking down a vent so they can’t turn around to see the back of the vent, this is fine at the start, however, when the player turns into a side vent, lets say it’s on the left, I want the restrictions to somehow rotate so that I can’t see the vent I just came through (The starting vent) but since the restrictions are the same I am still able to, however, what I am not able to is turn anymore to the left. I still don’t know if this makes sense but if nobody can help me then I might just have to work around the problem somehow.

Ohh sorry you want a left/Right and not an Up/Down! This is may a little bit difficulty! you want that a Player only can run forward and cant look back anymore if he is walked? Is that right?

Ohh and sorry for my bad english…