using Microsoft.MixedReality.Toolkit.UI.BoundsControl; using UnityEngine; /// /// 旋转转接头 /// public class TwistTheRotation : Device { private Vector2 ModelPos; /// /// 当前位置 /// private Vector2 currentPos; /// /// 上一帧位置 /// private Vector2 lsatPos; private Quaternion q; /// /// 旋转角度 /// private float RotateAngle; private Vector3 localEluer; /// /// 是否旋转 /// private bool IsSelet = false; /// /// 是否是顺时针 /// 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().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().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(); } }