using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using VRTK; using XFramework.NetOuter; //安装在支撑杆上触发松开 松开弹回尝试自锁的插销 [RequireComponent(typeof(VRTK_InteractableObject))] public class AutoFixedChaXiao : VRUsableStepActionBase { //use触发时 解锁位置 public Vector3 unFixedPos; //锁上后的位置 public Vector3 FixedPos; //use放弃后 若不满足回弹条件 被挡住时的位置 public Vector3 tryFixedPos; //影响回弹的杆件位置 public float rodPos; //能够回弹的杆件位置区间 X是较小值 Y较大值 public Vector2[] canFixedPos; FixatorStateEnum fixatorState= FixatorStateEnum.UnLocking; //插销状态变化事件 public event UnityAction FixatorStateChanged; public FixatorStateEnum FixatorState { get { return fixatorState; } private set { if (fixatorState != value) { fixatorState = value; FixatorStateChanged?.Invoke(fixatorState); } else { fixatorState = value; } } } bool unUsed=true; // Start is called before the first frame update // Update is called once per frame void FixedUpdate() { //if (Input.GetKey(KeyCode.Space)) //{ // unUsed = false; // this.transform.localPosition = unFixedPos; // FixatorState = FixatorStateEnum.UnLocking; //} //if (Input.GetKeyUp(KeyCode.Space)) //{ // unUsed = true; // this.transform.localPosition = tryFixedPos; //} if (fixatorState == FixatorStateEnum.UnLocking && unUsed)//打开状态 未使用手柄触发 尝试上锁 { float clampValue = 0; for (int i = 0; i < canFixedPos.Length; i++) { clampValue = Mathf.Clamp(rodPos, canFixedPos[i].x, canFixedPos[i].y); if (clampValue == rodPos)//在允许锁定的范围内 { FixatorState = FixatorStateEnum.Locking; this.transform.localPosition = FixedPos; } } } } public override void UnUsed() { unUsed = true; this.transform.localPosition = tryFixedPos; } public override void OnUsed() { unUsed = false; this.transform.localPosition = unFixedPos; FixatorState = FixatorStateEnum.UnLocking; } public override void Active(ProcessEnum stepName, UnityAction nodeFinishedEvent) { base.Active(stepName, nodeFinishedEvent); Debug.Log("插销激活"); } public override void UnActive(ProcessEnum stepName) { base.UnActive(stepName); Debug.Log("插销退出激活状态"); } }