LMQT/Assets/Scripts/touchControls/Board.cs
2024-12-10 09:03:45 +08:00

76 lines
2.8 KiB
C#

using UnityEngine;
using TouchScript.Gestures.TransformGestures;
/// <exclude />
public class Board : MonoBehaviour
{
//private PinnedTransformGesture gesture;
public TransformGesture gesture;
public TransformGesture ManipulationGesture;
public float rotateSpeed;
public float minScale;
public float maxScale;
public float minXRotation = -15f; // x轴的最小旋转角度
public float maxXRotation = 15f; // x轴的最大旋转角度
public float minZRotation = -15f; // z轴的最小旋转角度
public float maxZRotation = 15f; // z轴的最大旋转角度
private void OnEnable()
{
gesture.Transformed += transformedHandler;
ManipulationGesture.Transformed += manipulationTransformedHandler;
}
private void OnDisable()
{
gesture.Transformed -= transformedHandler;
ManipulationGesture.Transformed -= manipulationTransformedHandler;
}
private void transformedHandler(object sender, System.EventArgs e)
{
Debug.Log(gesture.DeltaPosition.x + "___" + gesture.DeltaPosition.y);
float y = gesture.DeltaPosition.x;// + obj.transform.rotation.y;
float x = gesture.DeltaPosition.y;//+ obj.transform.rotation.x;
transform.eulerAngles += new Vector3(0, -y * rotateSpeed, 0);
//obj.transform.Rotate(Vector3.up, -y * rotateSpeed, Space.World);
transform.Rotate(Vector3.right, x * rotateSpeed, Space.World);
// 获取当前物体的旋转
Vector3 currentRotation = transform.eulerAngles;
// 将x轴的旋转限制在指定范围内
currentRotation.x = ClampAngle(currentRotation.x, minXRotation, maxXRotation);
// 将z轴的旋转限制在指定范围内
currentRotation.z = ClampAngle(currentRotation.z, minZRotation, maxZRotation);
// 设置限制后的旋转
transform.eulerAngles = currentRotation;
}
private float ClampAngle(float angle, float min, float max)
{
if (angle < 90 || angle > 270) // 0-90和270-360的范围
{
if (angle > 180) angle -= 360; // 处理大于180度的情况
}
else // 90-270的范围
{
if (angle < 180) angle += 360; // 处理小于180度的情况
}
return Mathf.Clamp(angle, min, max);
}
private void manipulationTransformedHandler(object sender, System.EventArgs e)
{
Debug.Log(ManipulationGesture.DeltaScale);
transform.localScale += new Vector3(ManipulationGesture.DeltaScale - 1f, ManipulationGesture.DeltaScale - 1f, ManipulationGesture.DeltaScale - 1f);
transform.localScale = new Vector3(Mathf.Clamp(transform.localScale.x, minScale, maxScale), Mathf.Clamp(transform.localScale.y, minScale, maxScale), Mathf.Clamp(transform.localScale.z, minScale, maxScale));
}
}