Heroes_of_Hiis/Assets/Project Files/Scripts/JoonasP/LeverController.cs

103 lines
3.4 KiB
C#
Raw Permalink Normal View History

2022-03-15 18:07:59 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.XR.Interaction.Toolkit;
2022-03-15 18:07:59 +00:00
public class LeverController : MonoBehaviour
{
private Transform pivot; //The lever will use a pivot point
2022-03-18 18:35:45 +00:00
private Transform grabHand;
2022-03-15 18:07:59 +00:00
private bool leverGrabbed;
private bool eventCalled;
2022-03-18 18:35:45 +00:00
private bool leverDown;
2022-03-15 18:07:59 +00:00
private Vector3 handDelta;
private Vector3 lastPosition;
//These UnityEvent objects can be set in the inspector as any public methods from scripts
2022-03-15 18:07:59 +00:00
[SerializeField]
private UnityEvent onUp;
[SerializeField]
private UnityEvent onDown;
public float minAngle = 20f;
public float maxAngle = 75f;
public float hapticAmplitude = 1f;
public float hapticDuration = 0.1f;
2022-03-21 15:52:47 +00:00
public AudioSource upSound;
public AudioSource downSound;
2022-03-18 18:35:45 +00:00
//This script cannot work with multiplayer, see CheckHand() method
public Transform leftHand;
public Transform rightHand;
2022-03-15 18:07:59 +00:00
void Awake()
{
pivot = transform.parent;
leverGrabbed = false;
2022-03-18 18:35:45 +00:00
leverDown = false;
2022-03-15 18:07:59 +00:00
pivot.eulerAngles = new Vector3(pivot.eulerAngles.x, pivot.eulerAngles.y, minAngle);
2022-03-18 18:35:45 +00:00
leftHand = GameObject.Find("LeftHand Controller").transform;
rightHand = GameObject.Find("RightHand Controller").transform;
2022-03-15 18:07:59 +00:00
}
void Update()
{
if (leverGrabbed)
{
handDelta = lastPosition - grabHand.position; //The vector of hand movement between frames
pivot.RotateAround(pivot.position, pivot.forward, 200 * handDelta.y); //Is a Time.deltaTime multiplication needed here?
2022-03-15 18:07:59 +00:00
//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
2022-03-15 18:07:59 +00:00
if(pivot.eulerAngles.z < minAngle)
{
2022-03-18 18:35:45 +00:00
pivot.eulerAngles = new Vector3(pivot.eulerAngles.x, pivot.eulerAngles.y, minAngle);
if (leverDown)
2022-03-15 18:07:59 +00:00
{
onUp.Invoke();
grabHand.gameObject.GetComponent<ActionBasedController>().SendHapticImpulse(hapticAmplitude, hapticDuration);
2022-03-21 15:52:47 +00:00
if (upSound != null) upSound.Play();
2022-03-18 18:35:45 +00:00
leverDown = false;
2022-03-15 18:07:59 +00:00
}
}
else if(pivot.eulerAngles.z > maxAngle)
{
2022-03-18 18:35:45 +00:00
pivot.eulerAngles = new Vector3(pivot.eulerAngles.x, pivot.eulerAngles.y, maxAngle);
if (!leverDown)
2022-03-15 18:07:59 +00:00
{
onDown.Invoke();
grabHand.gameObject.GetComponent<ActionBasedController>().SendHapticImpulse(hapticAmplitude, hapticDuration);
2022-03-21 15:52:47 +00:00
if (downSound != null) downSound.Play();
2022-03-18 18:35:45 +00:00
leverDown = true;
2022-03-15 18:07:59 +00:00
}
}
lastPosition = grabHand.position;
}
}
2022-03-18 18:35:45 +00:00
public void GrabLever()
2022-03-15 18:07:59 +00:00
{
2022-03-18 18:35:45 +00:00
CheckHand();
2022-03-15 18:07:59 +00:00
leverGrabbed = !leverGrabbed;
handDelta = Vector3.zero;
lastPosition = grabHand.position;
eventCalled = false;
}
2022-03-18 18:35:45 +00:00
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;
}
2022-03-15 18:07:59 +00:00
}