lever improvements, ready to be used

This commit is contained in:
joonasp
2022-03-18 20:35:45 +02:00
parent 6f8245e798
commit 22e4260521
3 changed files with 286 additions and 204 deletions

View File

@@ -234,10 +234,10 @@ MonoBehaviour:
- m_Target: {fileID: 7543052554150911968}
m_TargetAssemblyTypeName: LeverController, Assembly-CSharp
m_MethodName: GrabLever
m_Mode: 2
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Transform, UnityEngine
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
@@ -249,10 +249,10 @@ MonoBehaviour:
- m_Target: {fileID: 7543052554150911968}
m_TargetAssemblyTypeName: LeverController, Assembly-CSharp
m_MethodName: GrabLever
m_Mode: 2
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Transform, UnityEngine
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
@@ -313,6 +313,8 @@ MonoBehaviour:
maxAngle: 75
hapticAmplitude: 1
hapticDuration: 0.1
leftHand: {fileID: 0}
rightHand: {fileID: 0}
--- !u!4 &7543052554459193268 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 417476, guid: 12c69a31240dca742abe09b6c9abc1f0,

View File

@@ -7,10 +7,10 @@ using UnityEngine.XR.Interaction.Toolkit;
public class LeverController : MonoBehaviour
{
private Transform pivot; //The lever will use a pivot point
private Transform grabHand; // ONLY ONE HAND WORKS RIGHT NOW. NEED A SOLUTION! SET HAND TRANSFORM IN onSelectedEntered()
// IN INTERACTION EVENTS UNTIL A BETTER SOLUTION
private Transform grabHand;
private bool leverGrabbed;
private bool eventCalled;
private bool leverDown;
private Vector3 handDelta;
private Vector3 lastPosition;
@@ -27,11 +27,18 @@ public class LeverController : MonoBehaviour
public float hapticAmplitude = 1f;
public float hapticDuration = 0.1f;
//This script cannot work with multiplayer, see CheckHand() method
public Transform leftHand;
public Transform rightHand;
void Awake()
{
pivot = transform.parent;
leverGrabbed = false;
leverDown = false;
pivot.eulerAngles = new Vector3(pivot.eulerAngles.x, pivot.eulerAngles.y, minAngle);
leftHand = GameObject.Find("LeftHand Controller").transform;
rightHand = GameObject.Find("RightHand Controller").transform;
}
void Update()
@@ -45,22 +52,22 @@ public class LeverController : MonoBehaviour
//Only one event is called per grab because the logic is in the update method
if(pivot.eulerAngles.z < minAngle)
{
pivot.eulerAngles = new Vector3(pivot.rotation.x, pivot.rotation.y, minAngle);
if (!eventCalled)
pivot.eulerAngles = new Vector3(pivot.eulerAngles.x, pivot.eulerAngles.y, minAngle);
if (leverDown)
{
onUp.Invoke();
grabHand.gameObject.GetComponent<ActionBasedController>().SendHapticImpulse(hapticAmplitude, hapticDuration);
eventCalled = true;
leverDown = false;
}
}
else if(pivot.eulerAngles.z > maxAngle)
{
pivot.eulerAngles = new Vector3(pivot.rotation.x, pivot.rotation.y, maxAngle);
if (!eventCalled)
pivot.eulerAngles = new Vector3(pivot.eulerAngles.x, pivot.eulerAngles.y, maxAngle);
if (!leverDown)
{
onDown.Invoke();
grabHand.gameObject.GetComponent<ActionBasedController>().SendHapticImpulse(hapticAmplitude, hapticDuration);
eventCalled = true;
leverDown = true;
}
}
@@ -68,14 +75,23 @@ public class LeverController : MonoBehaviour
}
}
public void GrabLever(Transform hand)
public void GrabLever()
{
CheckHand();
leverGrabbed = !leverGrabbed;
grabHand = hand;
handDelta = Vector3.zero;
lastPosition = grabHand.position;
eventCalled = false;
}
private void CheckHand()
{
float leftDistance = Vector3.Distance(leftHand.position, transform.position);
float rightDistance = Vector3.Distance(rightHand.position, transform.position);
if (leftDistance > rightDistance) grabHand = rightHand;
else grabHand = leftHand;
}
}