lever prefab, haptic events and more comments in lever code

This commit is contained in:
joonasp
2022-03-16 22:16:55 +02:00
parent ec8a34daf5
commit f2c0c3d9a1
4 changed files with 434 additions and 344 deletions

View File

@@ -2,18 +2,20 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
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!
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 bool leverGrabbed;
private bool eventCalled;
private Vector3 handDelta;
private Vector3 lastPosition;
//These UnityEvent objects can be set in the inspector as any public methods from scripts
[SerializeField]
private UnityEvent onUp;
[SerializeField]
@@ -22,6 +24,9 @@ public class LeverController : MonoBehaviour
public float minAngle = 20f;
public float maxAngle = 75f;
public float hapticAmplitude = 1f;
public float hapticDuration = 0.1f;
void Awake()
{
pivot = transform.parent;
@@ -34,14 +39,17 @@ public class LeverController : MonoBehaviour
if (leverGrabbed)
{
handDelta = lastPosition - grabHand.position; //The vector of hand movement between frames
pivot.RotateAround(pivot.position, pivot.forward, 200 * (handDelta.y));
pivot.RotateAround(pivot.position, pivot.forward, 200 * handDelta.y); //Is a Time.deltaTime multiplication needed here?
//Two following if statements set the up and down position angles and call events when angle is met
//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)
{
onUp.Invoke();
grabHand.gameObject.GetComponent<ActionBasedController>().SendHapticImpulse(hapticAmplitude, hapticDuration);
eventCalled = true;
}
}
@@ -51,6 +59,7 @@ public class LeverController : MonoBehaviour
if (!eventCalled)
{
onDown.Invoke();
grabHand.gameObject.GetComponent<ActionBasedController>().SendHapticImpulse(hapticAmplitude, hapticDuration);
eventCalled = true;
}
}