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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Not Allowing Camera To Rotate Past Certain Point

Discussion in 'Scripting' started by R-ty_dragon1, Jul 8, 2022.

  1. R-ty_dragon1

    R-ty_dragon1

    Joined:
    Aug 3, 2021
    Posts:
    50
    Hello!

    I'm back at the Unity Forum but this for another game! I would like my camera to follow the mouse but not to rotate past a certain point so the max I want it to rotate is sideways (180 degrees and -180 degrees)
    How would I do this?
    Here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraLook : MonoBehaviour
    6. {
    7.     public GameObject player;
    8.     public GameObject playerCamera;
    9.     public float sensitivity;
    10.     void FixedUpdate ()
    11.     {
    12.         float rotateHorizontal = Input.GetAxis ("Mouse X");
    13.         playerCamera.transform.RotateAround (player.transform.position, Vector3.up, rotateHorizontal * sensitivity);
    14.     }
    15. }
    16.  
    Thanks!
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Hello,

    How about using some checks to determine if the camera has reached, or passed, any of the two end points, and then make it stop going further in the wrong direction?
     
  3. R-ty_dragon1

    R-ty_dragon1

    Joined:
    Aug 3, 2021
    Posts:
    50
    I try that but it said a Quarternion can't use >