Skip to content

Commit

Permalink
Added rig and automatic rig change reactions for premade joystick sim…
Browse files Browse the repository at this point in the history
…ulator to avoid problems on scene changes
  • Loading branch information
Extrys committed Apr 18, 2024
1 parent 20247cb commit 5b3c3e1
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 22 deletions.
8 changes: 4 additions & 4 deletions Runtime/Prefabs/SimpleHandTrackingRig.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 7545014317440238945}
- component: {fileID: 7505831508167399543}
- component: {fileID: 3446690840397945026}
m_Layer: 0
m_Name: SimpleHandTrackingRig
m_TagString: Untagged
Expand All @@ -210,7 +210,7 @@ Transform:
- {fileID: 5738425329206812318}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &7505831508167399543
--- !u!114 &3446690840397945026
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
Expand All @@ -219,10 +219,10 @@ MonoBehaviour:
m_GameObject: {fileID: 8533970501455006381}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e0cb9aa70a22847b5925ee5f067c10a9, type: 3}
m_Script: {fileID: 11500000, guid: de1395b075e0d4643a0bb4d86ecd12ce, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Camera: {fileID: 2954705755887521368}
m_Camera: {fileID: 0}
m_OriginBaseGameObject: {fileID: 8533970501455006381}
m_CameraFloorOffsetObject: {fileID: 0}
m_RequestedTrackingOriginMode: 2
Expand Down
8 changes: 8 additions & 0 deletions Runtime/Scripts/Rigs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Runtime/Scripts/Rigs/SquirrelBytes.XRMasterHands.Rigs.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "SquirrelBytes.XRMasterHands.Rigs",
"rootNamespace": "",
"references": [
"GUID:dc960734dc080426fa6612f1c5fe95f3"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Runtime/Scripts/Rigs/XRMasterHandsRig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Unity.XR.CoreUtils;

public class XRMasterHandsRig : XROrigin
{
new void Awake()
{
base.Awake();
XRMasterHandsRigHandler.SetActiveRig(this);
}
}
11 changes: 11 additions & 0 deletions Runtime/Scripts/Rigs/XRMasterHandsRig.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Runtime/Scripts/Rigs/XRMasterHandsRigHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

public class XRMasterHandsRigHandler
{
static XRMasterHandsRig activeRig;
public static XRMasterHandsRig ActiveRig => activeRig;
public static event Action<XRMasterHandsRig> ActiveRigChanged;
public static void SetActiveRig(XRMasterHandsRig rig)
{
if(activeRig != rig)
{
activeRig = rig;
ActiveRigChanged?.Invoke(rig);
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Scripts/Rigs/XRMasterHandsRigHandler.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 16 additions & 16 deletions Runtime/Scripts/Simulators/PinchJoystickSimulator.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using Unity.XR.CoreUtils;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine;
using UnityEngine.XR.Hands;

public class PinchJoystickSimulator : MonoBehaviour, IJoystickSimulator
public class PinchJoystickSimulator : MonoBehaviour, IJoystickSimulator //TODO: Avoid using static Active rig, use signals instead
{
IStatePerformer joystickStarter;
public Vector3 beginPos;
public XROrigin rig;
public Transform character;
public float maxDistance = 0.2f;
public byte x,y;
Expand All @@ -21,16 +18,16 @@ public void Initialize(IStatePerformer statePerformer, XRHandJointsUpdatedEventA
{
this.joystickStarter = statePerformer;
joystickStarter.PerformStateChanged += OnPerformStateChanged;
rig =Object.FindObjectOfType<XROrigin>(true);
if(rig == null)
{
Debug.LogError("No XROrigin found in scene, PinchJoystickSimulator will not work, please add one rig on the scene");
enabled = false;
return;
}
character = rig.Origin.transform;
OnRigChanged(XRMasterHandsRigHandler.ActiveRig);
XRMasterHandsRigHandler.ActiveRigChanged += OnRigChanged;
this.jointsUpdatedEventArgs = jointsUpdatedEventArgs;
}
void OnRigChanged(XRMasterHandsRig rig)
{
enabled = rig;
if(rig != null)
character = rig.Origin.transform;
}

public void OnPerformStateChanged(bool performState)
{
Expand All @@ -57,7 +54,7 @@ void Update()
Vector3 normalized = vector / (length > 0 ? length : 1); ;
float clampedLength = Mathf.Clamp(length, 0, maxDistance);
Vector3 jtVector = normalized * clampedLength / maxDistance;
float viewAngle = Vector3.SignedAngle(Vector3.forward, rig.Camera.transform.forward, Vector3.up);
float viewAngle = Vector3.SignedAngle(Vector3.forward, XRMasterHandsRigHandler.ActiveRig.Camera.transform.forward, Vector3.up);
jtVector = Quaternion.Euler(0, -viewAngle + character.eulerAngles.y, 0) * jtVector;

x = (byte)((1 + jtVector.x) * 127);
Expand All @@ -74,6 +71,9 @@ void Update()
transform.forward = character.TransformDirection(endPos - startPos);
transform.localScale = new Vector3(1, 1, (endPos - startPos).magnitude);
}
private void OnDestroy() => joystickStarter.PerformStateChanged -= OnPerformStateChanged;

private void OnDestroy()
{
joystickStarter.PerformStateChanged -= OnPerformStateChanged;
XRMasterHandsRigHandler.ActiveRigChanged -= OnRigChanged;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ IJoystickSimulator GenerateSimulatorObject()
PinchJoystickSimulator pinchJoystickSimulator = arrow.AddComponent<PinchJoystickSimulator>();
pinchJoystickSimulator.gameObject.SetActive(false);
if(Application.isPlaying)
pinchJoystickSimulator.hideFlags = HideFlags.HideInHierarchy;
pinchJoystickSimulator.gameObject.hideFlags = HideFlags.HideInHierarchy;
return pinchJoystickSimulator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:dc960734dc080426fa6612f1c5fe95f3",
"GUID:ebd3a8b061c519548bac58d639001bfc",
"GUID:ce522b6ed64c8be4c989a1d26d0e3275"
"GUID:ce522b6ed64c8be4c989a1d26d0e3275",
"GUID:f45fee06433efa14e99670c8b71bf923"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down

0 comments on commit 5b3c3e1

Please sign in to comment.