Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How Do I Clamp the Camera?

Discussion in 'Scripting' started by RayanAziz11, Oct 22, 2022.

  1. RayanAziz11

    RayanAziz11

    Joined:
    Aug 4, 2021
    Posts:
    5
    I am making an arcade car game and I want to clamp the camera.. how do I do it? this is my script

    Code (CSharp):
    1. [Header("Main Stuff")]
    2.         public float sensitivity = 0.25f;
    3.         public float minVal = -60f;
    4.         public float maxVal = 60f;
    5.         public GameObject theCar;
    6.         public GameObject camHolder;
    7.        
    8.         float _mouseX;
    9.         float _mouseY;
    10.         Camera _cam;
    11.        
    12.         void Start()
    13.         {
    14.             _cam = Camera.main;
    15.         }
    16.  
    17.         void Update()
    18.         {
    19.             MouseInput();
    20.             CamHolderPosition();
    21.             CamHolderRotation();
    22.             CamLocalPos();
    23.  
    24.         }
    25.  
    26.         void MouseInput()
    27.         {
    28.             _mouseX = Mouse.current.delta.ReadValue().x * sensitivity;
    29.             _mouseY = Mouse.current.delta.ReadValue().y * sensitivity;
    30.         }
    31.  
    32.         void CamHolderPosition()
    33.         {
    34.             Vector3 carPos = theCar.transform.position;
    35.             camHolder.transform.position = new Vector3(
    36.                 carPos.x,
    37.                 carPos.y,
    38.                 carPos.z);
    39.         }
    40.  
    41.         void CamHolderRotation()
    42.         {
    43.             Quaternion camHolderRot = camHolder.transform.rotation;
    44.             Quaternion rot = Quaternion.Euler(camHolderRot.eulerAngles.x - _mouseY * sensitivity / 2,
    45.                 camHolderRot.eulerAngles.y + _mouseX * sensitivity,
    46.                 camHolderRot.eulerAngles.z);
    47.  
    48.             camHolderRot = Quaternion.Slerp(transform.rotation, rot, 1f);
    49.             camHolder.transform.rotation = camHolderRot;
    50.         }
    51.  
    52.         void CamLocalPos()
    53.         {
    54.             Vector3 camLocalPos = _cam.transform.localPosition;
    55.             if (!(_cam.transform.localPosition.z > -1f)) return;
    56.             camLocalPos = new Vector3(camLocalPos.x, camLocalPos.y, -1f);
    57.             _cam.transform.localPosition = camLocalPos;
    58.         }
     
  2. dynamicbutter

    dynamicbutter

    Joined:
    Jun 11, 2021
    Posts:
    61
    Hello @RayanAziz11. The CamLocalPos logic looks ok to me if your intention is to clamp the z position to less than or equal to -1. What's the problem you are seeing? One odd thing I notice is you have a cameraHolder but you are also directly modifying _cam. Is that the intent? If I drop the following script on my camera it clamps z to -1.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraClamp : MonoBehaviour
    4. {
    5.     public float zClamp = -1f;
    6.  
    7.     void Update()
    8.     {
    9.         if (transform.localPosition.z > zClamp) {
    10.             transform.localPosition = new Vector3(
    11.                 transform.localPosition.x, transform.localPosition.y, zClamp);
    12.         }
    13.     }
    14. }
     
    Last edited: Oct 22, 2022
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,717
  4. dynamicbutter

    dynamicbutter

    Joined:
    Jun 11, 2021
    Posts:
    61
    Cinemachine is awesome
     
    Kurt-Dekker likes this.