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

@@ -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;
}
}