- Home /
How to limit the angle of a camera with transform.RotateAround?
Okay, I've spent legitimately 3 days on this and I've become stumped by the 'transform.RotateAround' instruction. I'm trying to make a third person camera and it's all working fine except I can't figure out how to limit the Y Axis, so that it will stop if it gets too high or low around the character. Instead, what's happening now is if I move the camera too far it'll bug out at the 'poles' of the Y Axis.
 public class ThirdPersonCamera : MonoBehaviour
  {
      [SerializeField]
      private float distanceH;
      [SerializeField]
      private float distanceV;
      [SerializeField]
      private float smooth;
      [SerializeField]
      private Transform follow;
      [SerializeField]
      private float camSmoothDampTime = 0.1f;
      [Range (0f, 4f)][SerializeField]
      public float mouseSensitivityX = 1f;
      [Range(0f, 4f)][SerializeField]
      public float mouseSensitivityY = 1f;
  
      private Vector3 lookDir;
      private Vector3 restPosition;
      private Vector3 velocityCamSmooth = Vector3.zero;
  
      void Start()
      {
          follow = GameObject.FindWithTag("CameraTarget").transform;
      }
  
      void Update()
      {
          //Control with joystick
          transform.RotateAround(follow.transform.position, follow.up, mouseSensitivityX * Input.GetAxis("Joy X") * Time.deltaTime * 100);
          transform.RotateAround(follow.transform.position, follow.right, mouseSensitivityY * Input.GetAxis("Joy Y") * Time.deltaTime * 100);
  
          //Control with mouse
          transform.RotateAround(follow.transform.position, follow.up, mouseSensitivityX * Input.GetAxis("Mouse X") * Time.deltaTime * 100);
          transform.RotateAround(follow.transform.position, follow.right, mouseSensitivityY * Input.GetAxis("Mouse Y") * Time.deltaTime * 100);
      }
  
      void LateUpdate()
      {
          Vector3 characterOffset = follow.position;
  
          lookDir = characterOffset - this.transform.position;
          lookDir.Normalize();
  
          restPosition = characterOffset * distanceV - lookDir * distanceH;
          smoothPosition(this.transform.position, restPosition);
          transform.LookAt(follow);
      }
  
      void smoothPosition(Vector3 fromPos, Vector3 toPos)
      {
          this.transform.position = Vector3.SmoothDamp(fromPos, toPos, ref velocityCamSmooth, camSmoothDampTime);
      }
  }
This is what i have so far. Void Update is where the user controls are. I hope the internet can help. Thank you in advance!
Answer by toddisarockstar · May 17, 2017 at 11:03 PM
here is quick example of limiting an axis between two angles. Im just giving you am axaple for one axis. you whould need to write it again yourself for the X axis if thats what you want
 //adjust these next 2 floats to represent the angle where you stop.
     public float toplimit;
     public float bottomlimit;
 
 void Update(){
 
 
                 
                 float jy = Input.GetAxis ("Joy Y");
                  jy = jy + Input.GetAxis ("Mouse Y");
 
                 if (transform.eulerAngles.y > toplimit) {
                         if (jy > 0) {jy = 0;}}
                 if (transform.eulerAngles.y<bottomlimit) {
                 if(jy<0){jy=0;}}
 
                 transform.RotateAround(follow.transform.position, follow.right, mouseSensitivityY * jy * Time.deltaTime * 100);
 }
 
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                