From 73bde05f1bc7703cc6f397768294e111a988723e Mon Sep 17 00:00:00 2001 From: Duviz 2 <91156184+2duviz2@users.noreply.github.com> Date: Thu, 29 Aug 2024 23:25:07 +0200 Subject: [PATCH 1/3] Added vibration to the ship lever --- Source/Physics/Interactions/ShipLever.cs | 57 ++++++++++++++---------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/Source/Physics/Interactions/ShipLever.cs b/Source/Physics/Interactions/ShipLever.cs index 63229e0c..48fae972 100644 --- a/Source/Physics/Interactions/ShipLever.cs +++ b/Source/Physics/Interactions/ShipLever.cs @@ -1,6 +1,7 @@ -using LCVR.Assets; +using LCVR.Assets; using LCVR.Networking; using LCVR.Player; +using System; using System.Collections; using System.IO; using UnityEngine; @@ -10,7 +11,7 @@ namespace LCVR.Physics.Interactions; internal class ShipLeverInteractable : MonoBehaviour, VRInteractable { private ShipLever lever; - private VRInteractor currentInteractor; + private VRInteractor interactor; public InteractableFlags Flags => InteractableFlags.BothHands; @@ -21,8 +22,8 @@ private void Awake() private void Update() { - if (!lever.CanInteract && currentInteractor != null) - OnButtonRelease(currentInteractor); + if (!lever.CanInteract && interactor != null) + OnButtonRelease(interactor); } public bool OnButtonPress(VRInteractor interactor) @@ -30,18 +31,18 @@ public bool OnButtonPress(VRInteractor interactor) if (!lever.CanInteract || Plugin.Config.DisableShipLeverInteraction.Value) return false; - currentInteractor = interactor; + this.interactor = interactor; interactor.FingerCurler.ForceFist(true); - lever.StartInteracting(interactor.transform, ShipLever.Actor.Self); + lever.StartInteracting(interactor.transform, ShipLever.Actor.Self, interactor); return true; } public void OnButtonRelease(VRInteractor interactor) { - currentInteractor = null; + this.interactor = null; interactor.FingerCurler.ForceFist(false); @@ -62,6 +63,7 @@ public class ShipLever : MonoBehaviour private Channel channel; public bool CanInteract => lever.triggerScript.interactable && currentActor != Actor.Other; + public Actor CurrentActor => currentActor; private void Awake() { @@ -71,7 +73,7 @@ private void Awake() channel = NetworkSystem.Instance.CreateChannel(ChannelType.ShipLever); channel.OnPacketReceived += OnOtherInteractWithLever; } - + VRInteractor interactor; private void Update() { if (rotateTo == null) @@ -81,6 +83,9 @@ private void Update() var direction = rotateTo.TransformPoint(new Vector3(0.02f, 0.05f, 0f)) - transform.position; var eulerAngles = Quaternion.LookRotation(direction).eulerAngles; + if(interactor != null) + interactor.Vibrate(0.1f, 0.1f); + if (eulerAngles.y > 180) { eulerAngles.y = 270; @@ -101,12 +106,14 @@ private void Update() transform.eulerAngles = eulerAngles; } - public void StartInteracting(Transform target, Actor actor) + public void StartInteracting(Transform target, Actor actor, VRInteractor vrInteractor) { currentActor = actor; animator.enabled = false; rotateTo = target; - + interactor = vrInteractor; + + if (actor == Actor.Self) channel.SendPacket([1]); } @@ -117,7 +124,7 @@ public void StopInteracting() { if (shouldTrigger == TriggerDirection.LandShip && lever.playersManager.inShipPhase || shouldTrigger == TriggerDirection.DepartShip && !lever.playersManager.inShipPhase) { - StartCoroutine(PerformLeverAction(currentActor == Actor.Self)); + StartCoroutine(performLeverAction(currentActor == Actor.Self)); return; } @@ -135,7 +142,7 @@ public void StopInteracting() } } - private IEnumerator PerformLeverAction(bool isLocal) + private IEnumerator performLeverAction(bool isLocal) { if (isLocal) lever.LeverAnimation(); @@ -154,30 +161,32 @@ private void OnOtherInteractWithLever(ushort other, BinaryReader reader) switch (interacting) { - case true when currentActor == Actor.None: - StartInteracting(player.Bones.RightHand, Actor.Other); + case true when CurrentActor == ShipLever.Actor.None: + StartInteracting(player.Bones.RightHand, ShipLever.Actor.Other, null); break; - case false when currentActor == Actor.Other: + case false when CurrentActor == ShipLever.Actor.Other: StopInteracting(); break; } } - public static void Create() + public static ShipLever Create() { var startMatch = FindObjectOfType(); - startMatch.leverAnimatorObject.gameObject.AddComponent(); + var lever = startMatch.leverAnimatorObject.gameObject.AddComponent(); - if (!VRSession.InVR) - return; + if (VRSession.InVR) + { + var leverObject = startMatch.leverAnimatorObject.gameObject; + var interactable = Instantiate(AssetManager.Interactable, leverObject.transform); - var leverObject = startMatch.leverAnimatorObject.gameObject; - var interactable = Instantiate(AssetManager.Interactable, leverObject.transform); + interactable.transform.localPosition = new Vector3(0.2327f, 0.0404f, 11.6164f); + interactable.transform.localScale = new Vector3(1, 1, 4); - interactable.transform.localPosition = new Vector3(0.2327f, 0.0404f, 11.6164f); - interactable.transform.localScale = new Vector3(1, 1, 4); + interactable.AddComponent(); + } - interactable.AddComponent(); + return lever; } public enum Actor From cc4827bfd974f3d81677a7d1595e79cfff08e527 Mon Sep 17 00:00:00 2001 From: Duviz 2 <91156184+2duviz2@users.noreply.github.com> Date: Fri, 30 Aug 2024 12:29:15 +0200 Subject: [PATCH 2/3] Changed vibration code to the dev branch --- Source/Physics/Interactions/ShipLever.cs | 59 ++++++++++++------------ 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/Source/Physics/Interactions/ShipLever.cs b/Source/Physics/Interactions/ShipLever.cs index 48fae972..684407a4 100644 --- a/Source/Physics/Interactions/ShipLever.cs +++ b/Source/Physics/Interactions/ShipLever.cs @@ -1,7 +1,6 @@ using LCVR.Assets; using LCVR.Networking; using LCVR.Player; -using System; using System.Collections; using System.IO; using UnityEngine; @@ -11,7 +10,7 @@ namespace LCVR.Physics.Interactions; internal class ShipLeverInteractable : MonoBehaviour, VRInteractable { private ShipLever lever; - private VRInteractor interactor; + private VRInteractor currentInteractor; public InteractableFlags Flags => InteractableFlags.BothHands; @@ -22,8 +21,8 @@ private void Awake() private void Update() { - if (!lever.CanInteract && interactor != null) - OnButtonRelease(interactor); + if (!lever.CanInteract && currentInteractor != null) + OnButtonRelease(currentInteractor); } public bool OnButtonPress(VRInteractor interactor) @@ -31,7 +30,7 @@ public bool OnButtonPress(VRInteractor interactor) if (!lever.CanInteract || Plugin.Config.DisableShipLeverInteraction.Value) return false; - this.interactor = interactor; + currentInteractor = interactor; interactor.FingerCurler.ForceFist(true); @@ -42,7 +41,7 @@ public bool OnButtonPress(VRInteractor interactor) public void OnButtonRelease(VRInteractor interactor) { - this.interactor = null; + currentInteractor = null; interactor.FingerCurler.ForceFist(false); @@ -61,9 +60,10 @@ public class ShipLever : MonoBehaviour private TriggerDirection shouldTrigger = TriggerDirection.None; private Actor currentActor; private Channel channel; + VRInteractor CurrentInteractor; + float LastRot; public bool CanInteract => lever.triggerScript.interactable && currentActor != Actor.Other; - public Actor CurrentActor => currentActor; private void Awake() { @@ -73,7 +73,7 @@ private void Awake() channel = NetworkSystem.Instance.CreateChannel(ChannelType.ShipLever); channel.OnPacketReceived += OnOtherInteractWithLever; } - VRInteractor interactor; + private void Update() { if (rotateTo == null) @@ -83,9 +83,6 @@ private void Update() var direction = rotateTo.TransformPoint(new Vector3(0.02f, 0.05f, 0f)) - transform.position; var eulerAngles = Quaternion.LookRotation(direction).eulerAngles; - if(interactor != null) - interactor.Vibrate(0.1f, 0.1f); - if (eulerAngles.y > 180) { eulerAngles.y = 270; @@ -103,15 +100,21 @@ private void Update() shouldTrigger = eulerAngles.x > 290 ? TriggerDirection.DepartShip : TriggerDirection.None; } + if (CurrentInteractor != null && Mathf.Abs(LastRot - eulerAngles.y) >= 5) + { + CurrentInteractor.Vibrate(0.1f, 0.3f); + LastRot = eulerAngles.y; + } + transform.eulerAngles = eulerAngles; } - public void StartInteracting(Transform target, Actor actor, VRInteractor vrInteractor) + public void StartInteracting(Transform target, Actor actor, VRInteractor interactor = null) { currentActor = actor; animator.enabled = false; rotateTo = target; - interactor = vrInteractor; + CurrentInteractor = interactor; if (actor == Actor.Self) @@ -124,7 +127,7 @@ public void StopInteracting() { if (shouldTrigger == TriggerDirection.LandShip && lever.playersManager.inShipPhase || shouldTrigger == TriggerDirection.DepartShip && !lever.playersManager.inShipPhase) { - StartCoroutine(performLeverAction(currentActor == Actor.Self)); + StartCoroutine(PerformLeverAction(currentActor == Actor.Self)); return; } @@ -142,7 +145,7 @@ public void StopInteracting() } } - private IEnumerator performLeverAction(bool isLocal) + private IEnumerator PerformLeverAction(bool isLocal) { if (isLocal) lever.LeverAnimation(); @@ -161,32 +164,30 @@ private void OnOtherInteractWithLever(ushort other, BinaryReader reader) switch (interacting) { - case true when CurrentActor == ShipLever.Actor.None: - StartInteracting(player.Bones.RightHand, ShipLever.Actor.Other, null); + case true when currentActor == Actor.None: + StartInteracting(player.Bones.RightHand, Actor.Other); break; - case false when CurrentActor == ShipLever.Actor.Other: + case false when currentActor == Actor.Other: StopInteracting(); break; } } - public static ShipLever Create() + public static void Create() { var startMatch = FindObjectOfType(); - var lever = startMatch.leverAnimatorObject.gameObject.AddComponent(); + startMatch.leverAnimatorObject.gameObject.AddComponent(); - if (VRSession.InVR) - { - var leverObject = startMatch.leverAnimatorObject.gameObject; - var interactable = Instantiate(AssetManager.Interactable, leverObject.transform); + if (!VRSession.InVR) + return; - interactable.transform.localPosition = new Vector3(0.2327f, 0.0404f, 11.6164f); - interactable.transform.localScale = new Vector3(1, 1, 4); + var leverObject = startMatch.leverAnimatorObject.gameObject; + var interactable = Instantiate(AssetManager.Interactable, leverObject.transform); - interactable.AddComponent(); - } + interactable.transform.localPosition = new Vector3(0.2327f, 0.0404f, 11.6164f); + interactable.transform.localScale = new Vector3(1, 1, 4); - return lever; + interactable.AddComponent(); } public enum Actor From dbd0dc7e12ae30a398680779e008caedfa5b75d2 Mon Sep 17 00:00:00 2001 From: Duviz 2 <91156184+2duviz2@users.noreply.github.com> Date: Fri, 30 Aug 2024 13:31:51 +0200 Subject: [PATCH 3/3] Changed vibration system a little bit. And fixed the lever rotation as an extra Changed the angle from y to x Added vibration the pulling the lever Clamped the levers rotation to avoid it from clipping --- Source/Physics/Interactions/ShipLever.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Source/Physics/Interactions/ShipLever.cs b/Source/Physics/Interactions/ShipLever.cs index 684407a4..83b88dd8 100644 --- a/Source/Physics/Interactions/ShipLever.cs +++ b/Source/Physics/Interactions/ShipLever.cs @@ -61,7 +61,7 @@ public class ShipLever : MonoBehaviour private Actor currentActor; private Channel channel; VRInteractor CurrentInteractor; - float LastRot; + float LastVibrateRotation; public bool CanInteract => lever.triggerScript.interactable && currentActor != Actor.Other; @@ -100,10 +100,12 @@ private void Update() shouldTrigger = eulerAngles.x > 290 ? TriggerDirection.DepartShip : TriggerDirection.None; } - if (CurrentInteractor != null && Mathf.Abs(LastRot - eulerAngles.y) >= 5) + eulerAngles.x = Mathf.Clamp(eulerAngles.x, 270, 310); //Fixes the lever from clipping if you rotate it too much + + if (CurrentInteractor != null && Mathf.Abs(LastVibrateRotation - eulerAngles.x) >= 10) //Vibrate the controller if the lever has been rotated too much { CurrentInteractor.Vibrate(0.1f, 0.3f); - LastRot = eulerAngles.y; + LastVibrateRotation = eulerAngles.x; } transform.eulerAngles = eulerAngles; @@ -147,6 +149,8 @@ public void StopInteracting() private IEnumerator PerformLeverAction(bool isLocal) { + if(CurrentInteractor != null) + CurrentInteractor.Vibrate(.3f, 0.5f); if (isLocal) lever.LeverAnimation(); yield return new WaitForSeconds(1.67f);