Limit camera rotation?

Just recently started working in Unity with VR/Oculus, and I was wondering if it is possible to set constraints on the angles of rotation for the viewing camera… for example, if I want the camera to emulate an actual head of a character, then the only way you should be able to see behind you is to move your body (not just your head, unless you are an owl); ie. you would only have about a 270 degree field of rotation (not full 360 degrees)

Could I do this with some scripting?

We don’t seem to be able to directly control the rotation of the camera in VR mode. Not sure if it’s a bug or enforcing of VR best practices which highly discourage taking control of the camera. My guess is on the latter.

You could probably put the camera as child and counter-rotate the parent to negate the undesired rotation but that sounds messy.

In my opinion, what you probably should be doing instead is to black out the player’s view when crossing the threshold.

Here’s what I’ve done.

Not sure if there’s a better way to do this, but to turn the image “off” you’ll first need to:

  • Add a Canvas to your camera.
  • Set its render mode to Screen Space - Camera
  • Set the Render Camera to your camera
  • set the plane distance to 1.
  • Add a (UI → Image) element to your canvas.
  • Add the following script to your camera.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class LimitRotation : MonoBehaviour {

    public float rotationLimit = 90f; // Rotation limit in either direction

    private Image blackout;

    void Start () {
        blackout = GetComponentInChildren<Image>();
    }
  
    void Update () {
        float cameraRotationY = transform.localRotation.eulerAngles.y;

        if (cameraRotationY >= rotationLimit && cameraRotationY < (360-rotationLimit)) {
            blackout.color = Color.black;
        } else {
            blackout.color = Color.clear;
        }
    }
}

That should do the trick.

1 Like

Hmm, you’re right that there would be problems if someone DID turn around too far, if I were to freeze or lock the camera at a different angle, that would be unnatural and would seem glitchy, and ruin the VR experience. So it’s probably not a good practice. Maybe I will just position it such that the model (body/neck) are blocking the view. (although… I’m also wondering if there’s a way to change the “clipping” radius of the camera… not sure if that’s the best term for it, but when I am viewing inside and enclosed space, the camera view seems to go right through the walls. For example, I should be able to look around a 10’ high x 5’ wide enclosure with no problem, but parts of the walls/ceiling end up becoming clipped or “invisible” as if the camera has poked through them…

This also causes problems with clipping of the body mesh so close to the camera. So… any way to adjust this “radius” so that it is very small?

Simply reduce the near clipping plane on your camera through the inspector.

Great… it’s been a couple years since using Unity, I’m a bit of a neophyte again… (and I never did use the camera clipping for the first game I made, a mobile app. Guess I just didn’t need to…)