112 lines
2.9 KiB
C#
112 lines
2.9 KiB
C#
using Microsoft.MixedReality.Toolkit.UI.BoundsControl;
|
|
using UnityEngine;
|
|
/// <summary>
|
|
/// 旋转转接头
|
|
/// </summary>
|
|
public class TwistTheRotation : Device
|
|
{
|
|
private Vector2 ModelPos;
|
|
/// <summary>
|
|
/// 当前位置
|
|
/// </summary>
|
|
private Vector2 currentPos;
|
|
/// <summary>
|
|
/// 上一帧位置
|
|
/// </summary>
|
|
private Vector2 lsatPos;
|
|
private Quaternion q;
|
|
/// <summary>
|
|
/// 旋转角度
|
|
/// </summary>
|
|
private float RotateAngle;
|
|
private Vector3 localEluer;
|
|
/// <summary>
|
|
/// 是否旋转
|
|
/// </summary>
|
|
private bool IsSelet = false;
|
|
/// <summary>
|
|
/// 是否是顺时针
|
|
/// </summary>
|
|
public bool isClockwise = false;
|
|
|
|
// Start is called before the first frame update
|
|
public override void Start()
|
|
{
|
|
base.Start();
|
|
ModelPos = transform.position;
|
|
transform.localEulerAngles = localEluer;
|
|
GetComponent<BoundsControl>().Active = false;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (IsSelet)
|
|
{
|
|
currentPos = transform.GetChild(0).position;
|
|
RotateAngle = Vector2.Angle(lsatPos - ModelPos, currentPos - ModelPos);
|
|
|
|
if (RotateAngle == 0)
|
|
{
|
|
lsatPos = currentPos;
|
|
|
|
}
|
|
else
|
|
{
|
|
float k;
|
|
q = Quaternion.FromToRotation(lsatPos - ModelPos, currentPos - ModelPos);
|
|
if (isClockwise)
|
|
{
|
|
k = q.z > 0 ? 1 : -1;
|
|
}
|
|
else
|
|
{
|
|
k = q.z < 0 ? 1 : -1;
|
|
|
|
}
|
|
localEluer.x += k * RotateAngle;
|
|
localEluer.x = Mathf.Clamp(localEluer.x, -2800, 0);
|
|
Debug.Log(localEluer.x);
|
|
transform.localEulerAngles = localEluer;
|
|
if (transform.localPosition.x < -0.06f || transform.localPosition.x > -0.072f)
|
|
{
|
|
transform.localPosition -= new Vector3(k * 0.00004f, 0, 0);
|
|
}
|
|
lsatPos = currentPos;
|
|
if (localEluer.x == -2800)
|
|
{
|
|
GetComponent<BoundsControl>().Active = false;
|
|
transform.localEulerAngles = new Vector3(0, 0, 0);
|
|
isCompleted = true;
|
|
}
|
|
if (localEluer.x == 0)
|
|
{
|
|
transform.localEulerAngles = new Vector3(0, 0, 0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void MyRotate()
|
|
{
|
|
|
|
|
|
}
|
|
public void RotateStarted()
|
|
{
|
|
IsSelet = true;
|
|
lsatPos = currentPos = transform.GetChild(0).position;
|
|
print("start");
|
|
}
|
|
public void RotateStoped()
|
|
{
|
|
IsSelet = false;
|
|
print("stop");
|
|
}
|
|
public override void Init()
|
|
{
|
|
base.Init();
|
|
|
|
}
|
|
}
|