Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Clamp orbit camera rotation

Discussion in 'Scripting' started by Demdem, May 19, 2019.

  1. Demdem

    Demdem

    Joined:
    Jun 2, 2017
    Posts:
    1
    Hello,
    Working on mobile, I want to move the camera using touch slide and pinch around a target. For now everything is working well, I can move the camera around the object and zoom in or out. there is the code used to move the cam around:
    Code (CSharp):
    1. //touchMagnitude is touch position delta, get the angle rotation over axis
    2.       Quaternion camTurn = Quaternion.AngleAxis(touchMagnitude.x, Vector3.up);
    3.       Quaternion camTurnHeight = Quaternion.AngleAxis(touchMagnitude.y, Vector3.zero + transform.right);
    4.  
    5.       Quaternion rotation = camTurn * camTurnHeight;
    6.  
    7. // add the rotation to the offset between the camera and the target
    8.       cameraOffset = rotation * cameraOffset;
    9.  
    10.       transform.position = target.position + cameraOffset;
    What I want to do now is to clamp the camera deplacement in height, because when the camera go over the gameobject it have a weird deplacement behaviour.
    And there it's going bad...
    I've tryed two options :
    1) limit the quaternion rotation, to not be over a value, so : How to clamp a quaternion ? if we talk in degrees, I want to clamp the x rotation between -80 and 80.
    2) check the angle made by the camera looking to the target, to not move height if the angle is equal to 80°.

    I've tryed everything about this two options so if somebody can give a way to reach what I looking for, it will save my week.