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 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.
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.
Source: https://gamedevsolutions.com/clamp-camera-rotation-unity/
Posted by: mantoothtionce.blogspot.com
0 Response to "How To Clamp Camera Rotation"
Post a Comment