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

61 lines
2.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using TouchScript.Gestures.TransformGestures;
using UnityEngine;
public class bodyR : MonoBehaviour
{
// Start is called before the first frame update
public TransformGesture gesture;
public GameObject obj;
public float rotateSpeed;
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;
}
private void OnDisable()
{
gesture.Transformed -= transformedHandler;
}
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;
obj.transform.eulerAngles += new Vector3(0, -y * rotateSpeed, 0);
//obj.transform.Rotate(Vector3.up, -y * rotateSpeed, Space.World);
obj.transform.Rotate(Vector3.right, x * rotateSpeed, Space.World);
// 获取当前物体的旋转
Vector3 currentRotation = obj.transform.eulerAngles;
// 将x轴的旋转限制在指定范围内
currentRotation.x = ClampAngle(currentRotation.x, minXRotation, maxXRotation);
// 将z轴的旋转限制在指定范围内
currentRotation.z = ClampAngle(currentRotation.z, minZRotation, maxZRotation);
// 设置限制后的旋转
obj.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);
}
}