Search Unity

Question Limiting first person camera rotation

Discussion in 'Entity Component System' started by Eldarado123, Oct 27, 2021.

  1. Eldarado123

    Eldarado123

    Joined:
    May 17, 2021
    Posts:
    7
    Hello, I'm trying to make a first person game with DOTS and I'm having trouble figuring out how to limit the vertical camera rotation so that the player can't look down into their body or too far in the opposite direction. I have a playerBody entity and a playerHead entity which is a child of the body, and I currently use this line of code to do the rotation using the head's rotation component as well as a couple of my own components:

    rotation.Value = math.mul(rotation.Value, quaternion.RotateX(-inputData.camera.y * sens.value * deltaTime));

    It works well, but I'm not sure how to stop it when looking straight down or straight up. Any attempts I've made so far to achieve this have not worked as I'd hoped. Any wisdom would be appreciated.
     
  2. Trumped

    Trumped

    Joined:
    Jul 3, 2017
    Posts:
    5
    Mathf.clamp is your best bet look up the unity documention and it explains how it works.
     
  3. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    You'll want to have a RotationEulerZXY component on the entity with the camera. The RotatationEulerSystem should automatically set the Rotation based on RotationEulerZXY. Then you can clamp the y component of the RotationEulerZXY. For better performance, use Unity.Mathematics.math.clamp and NOT UnityEngine.Mathf.Clamp
    Code (CSharp):
    1. rotationEuler.Value.y += -inputData.camera.y * sens.value * deltaTime;
    2. rotationEuler.Value.y = math.clamp(rotationEuler.Value.y, math.radians(-90), math.radians(90));
     
    Eldarado123 likes this.
  4. Eldarado123

    Eldarado123

    Joined:
    May 17, 2021
    Posts:
    7
    Thanks! This works perfectly. The RotationEulerZXY was a very key piece I was missing.