using UnityEngine;
|
using UnityEngine.InputSystem.Controls;
|
|
public class LeverController : MonoBehaviour
|
{
|
public enum Axis
|
{
|
forward,
|
right,
|
up,
|
}
|
public Axis RotationAxis;
|
public float maxAngle = 45f; // ·¹¹öÀÇ ÃÖ´ë °¢µµ
|
public float minAngle = -45f; // ·¹¹öÀÇ ÃÖ¼Ò °¢µµ
|
public float sensitivity = 0.2f; // ¸¶¿ì½º ¿òÁ÷ÀÓ¿¡ ´ëÇÑ ¹Î°¨µµ
|
private Vector3 rotationAxis; // ȸÀüÃà (±âº»°ª: XÃà)
|
|
private float currentAngle;
|
private Vector3 lastMousePosition;
|
|
|
/// <summary>
|
/// µå·¡±× °¡´É ¿©ºÎ
|
/// </summary>
|
private bool isDragging = false;
|
/// <summary>
|
/// ·¹¹ö°¡ ¹Ø¿¡ ÀÖ´ÂÁö ¾Æ·¡ÀÖ´ÂÁö ÆÇ´Ü
|
/// </summary>
|
private bool isDown = false;
|
private bool isRun = false;
|
private float startMouseY;
|
private float startAngle;
|
|
|
private int layerMask;
|
|
|
private float time = 0;
|
private Quaternion downTargetRos;
|
private Quaternion upTargetRos;
|
private float speed = 10;
|
private void Start()
|
{
|
Quaternion q = transform.localRotation;
|
layerMask = 1 << LayerMask.NameToLayer("Train");
|
if (RotationAxis == Axis.forward)
|
{
|
rotationAxis = Vector3.forward;
|
downTargetRos = q * Quaternion.Euler(0,0, maxAngle);
|
upTargetRos =q * Quaternion.Euler(0, 0, minAngle);
|
|
}
|
else if (RotationAxis == Axis.right)
|
{
|
rotationAxis = Vector3.right;
|
downTargetRos = q * Quaternion.Euler(maxAngle, 0, 0);
|
upTargetRos = q * Quaternion.Euler(minAngle, 0, 0);
|
}
|
else if (RotationAxis == Axis.up)
|
{
|
rotationAxis = Vector3.up;
|
downTargetRos = q * Quaternion.Euler(0, maxAngle, 0);
|
upTargetRos = q * Quaternion.Euler(0, minAngle, 0);
|
}
|
|
currentAngle = GetCurrentAngle();
|
}
|
|
|
public void OnDrag()
|
{
|
isDragging = true;
|
lastMousePosition = Input.mousePosition;
|
currentAngle = GetCurrentAngle(); // ÇöÀç
|
}
|
|
public void OffDrag()
|
{
|
isDragging = false;
|
}
|
|
public void OnClick()
|
{
|
isRun = true;
|
isDown = !isDown;
|
}
|
|
private void Update()
|
{
|
if (isDragging)
|
{
|
Vector3 mouseDelta = Input.mousePosition - lastMousePosition;
|
float angleDelta = mouseDelta.y * sensitivity;
|
float newAngle = currentAngle - angleDelta;
|
newAngle = Mathf.Clamp(newAngle, minAngle, maxAngle);
|
|
ApplyRotation(newAngle);
|
|
lastMousePosition = Input.mousePosition;
|
currentAngle = newAngle;
|
}
|
if (isRun)
|
{
|
time = Time.deltaTime * speed;
|
if (isDown)
|
{
|
transform.localRotation = Quaternion.Slerp(transform.localRotation, upTargetRos, time);
|
}
|
else
|
{
|
transform.localRotation = Quaternion.Slerp(transform.localRotation, downTargetRos, time);
|
}
|
|
}
|
}
|
|
private float GetCurrentAngle()
|
{
|
Vector3 currentRotation = transform.localRotation.eulerAngles;
|
float angle = 0f;
|
|
if (RotationAxis == Axis.forward)
|
angle = currentRotation.z;
|
else if (RotationAxis == Axis.right)
|
angle = currentRotation.x;
|
else if (RotationAxis == Axis.up)
|
angle = currentRotation.y;
|
|
// -180¿¡¼ 180 »çÀÌÀÇ °¢µµ·Î º¯È¯
|
if (angle > 180) angle -= 360;
|
|
return angle;
|
}
|
|
private void ApplyRotation(float angle)
|
{
|
Vector3 currentRotation = transform.localRotation.eulerAngles;
|
|
if (RotationAxis == Axis.forward)
|
currentRotation.z = angle;
|
else if (RotationAxis == Axis.right)
|
currentRotation.x = angle;
|
else if (RotationAxis == Axis.up)
|
currentRotation.y = angle;
|
|
transform.localRotation = Quaternion.Euler(currentRotation);
|
}
|
}
|