Select Page

the cursor

using UnityEngine;

public class GyroCamController : MonoBehaviour
{
private Vector3 initialRotation;
private float maxRotationDegrees = 2f;

void Start()
{
initialRotation = transform.localRotation.eulerAngles;
}

void OnMouseDrag()
{
Vector3 mouseCurrentPosition = Input.mousePosition;
float xOffset = mouseCurrentPosition.x – initialRotation.x;
float yOffset = mouseCurrentPosition.y – initialRotation.y;

Vector3 rotX = new Vector3(xOffset, 0f, 0f);
Vector3 rotY = new Vector3(0f, yOffset, 0f);

transform.localRotation = Quaternion.Euler(transform.localRotation.eulerAngles + Clamping(rotX, maxRotationDegrees));
transform.localRotation = Quaternion.Euler(transform.localRotation.eulerAngles + Clamping(rotY, maxRotationDegrees));
}

Vector3 Clamping(Vector3 vec3, float max)
{
if(vec3.x > max || vec3.x < -max) { vec3.x = Mathf.Clamp(vec3.x,-max,max); } if(vec3.y > max || vec3.y < -max) { vec3.y = Mathf.Clamp(vec3.y,-max,max); } if(vec3.z > max || vec3.z < -max) { vec3.z = Mathf.Clamp(vec3.z,-max,max); } return vec3; } }