Firstborn/Assets/InfinityPBR/_InfinityPBR - Locks and Lo.../Scripts/LockObjectRotation.cs

62 lines
2.3 KiB
C#
Raw Permalink Normal View History

using UnityEngine;
public class LockObjectRotation : MonoBehaviour
{
public bool mouseMovementOn = false;
[SerializeField] private float maxX = 30f; // Max rotation
[SerializeField] private float maxY = 30f; // Max rotation
[SerializeField] private Vector3 mousePosition; // last position of mouse
[SerializeField] private float speed = 2160f; // speed of rotation
[SerializeField] private float startX; // Starting rotation
[SerializeField] private float startY; // Starting rotation
void Start()
{
startX = GetAngle(transform.localEulerAngles.x);
startY = GetAngle(transform.localEulerAngles.y);
}
void Update()
{
MouseMovement();
mousePosition = Camera.main.ScreenToViewportPoint(Input.mousePosition); // Save mouse position every frame.
}
private void MouseMovement()
{
// If we are holding down the left mouse button...
if (Input.GetMouseButton(0) || mouseMovementOn)
{
// Grab the new position of the mouse, 0,0 - 1,1
Vector3 newPosition = Camera.main.ScreenToViewportPoint(Input.mousePosition);
// Compute the delta in the x and y positions. Do inversion for the yDelta. Note X/Y are flipped.
float horDelta = -(newPosition.x - mousePosition.x) * speed * Time.deltaTime;
float verDelta = (newPosition.y - mousePosition.y) * speed * Time.deltaTime;
// Make sure we don't go over our max range
if (GetAngle(transform.localEulerAngles.x) + verDelta > startX + maxX || GetAngle(transform.localEulerAngles.x) + verDelta < startX - maxX)
verDelta = 0;
if (GetAngle(transform.localEulerAngles.y) + horDelta > startY + maxY || GetAngle(transform.localEulerAngles.y) + horDelta < startY - maxY)
horDelta = 0;
// Compute & assign the new angle
Vector3 newAngles = new Vector3(transform.localEulerAngles.x + verDelta,
transform.localEulerAngles.y + horDelta, transform.localEulerAngles.z);
transform.localEulerAngles = newAngles;
}
}
private float GetAngle(float eulerAngle)
{
float angle = eulerAngle;
angle %= 360;
if (angle > 180)
angle -= 360;
return angle;
}
}