Search Unity

How can I make the camera orbit around the player also on Y up/down 360 degrees with clamp?

Discussion in 'Scripting' started by Chocolade, Aug 4, 2020.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933
    The script is attached to the Main Camera.

    Clamp I mean that it wont orbit down inside the floor and not orbit up back the player to limit it to 90 and -90 degrees on the Y I think.

    This is the original script that work only to the left and right :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerFollow : MonoBehaviour
    7. {
    8.     public Transform PlayerTransform;
    9.  
    10.     private Vector3 _cameraOffset;
    11.  
    12.     [Range(0.01f, 1.0f)]
    13.     public float SmoothFactor = 0.5f;
    14.  
    15.     public bool LookAtPlayer = false;
    16.  
    17.     public bool RotateAroundPlayer = true;
    18.  
    19.     public float RotationsSpeed = 5.0f;
    20.  
    21.     // Use this for initialization
    22.     void Start()
    23.     {
    24.         _cameraOffset = transform.position - PlayerTransform.position;
    25.     }
    26.  
    27.     // LateUpdate is called after Update methods
    28.     void LateUpdate()
    29.     {
    30.  
    31.         if (RotateAroundPlayer)
    32.         {
    33.             Quaternion camTurnAngle =
    34.                 Quaternion.AngleAxis(Input.GetAxis("Mouse X") * RotationsSpeed, Vector3.up);
    35.  
    36.             _cameraOffset = camTurnAngle * _cameraOffset;
    37.         }
    38.  
    39.         Vector3 newPos = PlayerTransform.position + _cameraOffset;
    40.  
    41.         transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);
    42.  
    43.         if (LookAtPlayer || RotateAroundPlayer)
    44.             transform.LookAt(PlayerTransform);
    45.     }
    46. }
    47.  

    This is what I tried and it seems to be working but also if I first move the camera to the left right if not and first moving up down it will move it opposite directions up will be down and down will be up only when moving it to the left and right then it fix the up down directions :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerFollow : MonoBehaviour
    7. {
    8.     public Transform PlayerTransform;
    9.  
    10.     private Vector3 _cameraOffset;
    11.  
    12.     [Range(0.01f, 1.0f)]
    13.     public float SmoothFactor = 0.5f;
    14.  
    15.     public bool LookAtPlayer = false;
    16.  
    17.     public bool RotateAroundPlayer = true;
    18.  
    19.     public float RotationsSpeed = 5.0f;
    20.  
    21.     // Use this for initialization
    22.     void Start()
    23.     {
    24.         _cameraOffset = transform.position - PlayerTransform.position;
    25.     }
    26.  
    27.     // LateUpdate is called after Update methods
    28.     void LateUpdate()
    29.     {
    30.  
    31.         if (RotateAroundPlayer)
    32.         {
    33.             Quaternion camTurnAngle =
    34.                 Quaternion.AngleAxis(Input.GetAxis("Mouse X") * RotationsSpeed, Vector3.up);
    35.  
    36.             Quaternion camTurnAngle1 =
    37.                 Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * RotationsSpeed, Vector3.right);
    38.  
    39.             _cameraOffset = camTurnAngle * _cameraOffset;
    40.             _cameraOffset = camTurnAngle1 * _cameraOffset;
    41.         }
    42.  
    43.         Vector3 newPos = PlayerTransform.position + _cameraOffset;
    44.  
    45.         transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);
    46.  
    47.         if (LookAtPlayer || RotateAroundPlayer)
    48.             transform.LookAt(PlayerTransform);
    49.     }
    50. }
    51.