banner



How To Clamp Camera Rotation

In this commodity, I will prove you lot how yous can clamp camera rotation in unity in Android or iOS Mobile.

Here is a uncomplicated script of the camera clamping in unity that will work on any platform similar PC, Mobile devices.

clamp camera rotation in android unity
clamp photographic camera rotation in android unity

Clamp camera rotation C# script

            using UnityEngine;  public class CameraRotaionWithClamping: MonoBehaviour {      [Header("Min Max Value Of Vertical Rotation")]     public bool isClampVerticalRotation;     public float verticalMinValue;     public float verticalMaxValue;      [Infinite()]     [Header("Min Max Value Of Horizontal Rotation")]     public bool isClampHorizontalRotation;     public float horizontalMinValue;     public float horizontalMaxValue;      private Vector3 firstpoint;      private Vector3 secondpoint;      private bladder xAngle;      individual float yAngle;     private float xAngTemp;     private float yAngTemp;      // Booleans     private bool isUpdatePosition = imitation;      // Update is chosen once per frame     void Update()     {         if(Input.GetMouseButtonDown(0))         {             OnStartTouch();             isUpdatePosition = true;         }          if(Input.GetMouseButtonUp(0))         {             isUpdatePosition = fake;         }                  if(isUpdatePosition)         {             UpdatePosition();         }     }      individual void OnStartTouch()     {         firstpoint = Input.mousePosition;          xAngTemp = xAngle;         yAngTemp = yAngle;     }      public void UpdatePosition()     {         secondpoint = Input.mousePosition;          xAngle = xAngTemp + (secondpoint.x - firstpoint.ten) * 180.0f / Screen.width;         yAngle = yAngTemp - (secondpoint.y - firstpoint.y) * 90.0f / Screen.height;          // Clamping Vertical Value         if (isClampVerticalRotation)         {             yAngle = Mathf.Clench(yAngle, verticalMinValue, verticalMaxValue);         }          // Clamping Horizontal Value         if (isClampHorizontalRotation)         {             xAngle = Mathf.Clamp(xAngle, horizontalMinValue, horizontalMaxValue);         }          transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);     } }          

Attach this script to the photographic camera commencement, So in society to clamp camera rotation set the camera clamping values in the unity editor

You tin can decide in which directions yous desire to clench your photographic camera whether horizontally or vertically.

In whatsoever example, don't forget to check the bool of direction in which you want to clench your camera on your android mobile.

Hither is a sample of my script

How to clamp camera rotation in unity
Clamp camera rotation

Then here equally y'all can run across I wanted my camera to be clamped in both directions X and Y.

Then get-go I checked both of the booleans to clamp the photographic camera in both directions so I gear up the Min and Max values of the camera rotation.

In example you don't desire to clamp the photographic camera in both directions you tin can cheque the boolean of the particular direction In which you want your camera to clench and check the boolean Fake to rotate the camera without clamping.

I hope guys this article was helpful for you, Annotate your thoughts or if yous have whatsoever questions, problems, or any query please experience free to comment, Thank you.

How exercise you clamp the camera rotation in unity? Let me know in the comment section.

Source: https://gamedevsolutions.com/clamp-camera-rotation-unity/

Posted by: mantoothtionce.blogspot.com

0 Response to "How To Clamp Camera Rotation"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel