initial lever system
This commit is contained in:
72
Assets/Project Files/Scripts/JoonasP/LeverController.cs
Normal file
72
Assets/Project Files/Scripts/JoonasP/LeverController.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
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 bool leverGrabbed;
|
||||
|
||||
private bool eventCalled;
|
||||
|
||||
private Vector3 handDelta;
|
||||
private Vector3 lastPosition;
|
||||
|
||||
[SerializeField]
|
||||
private UnityEvent onUp;
|
||||
[SerializeField]
|
||||
private UnityEvent onDown;
|
||||
|
||||
public float minAngle = 20f;
|
||||
public float maxAngle = 75f;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
pivot = transform.parent;
|
||||
leverGrabbed = false;
|
||||
pivot.eulerAngles = new Vector3(pivot.eulerAngles.x, pivot.eulerAngles.y, minAngle);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (leverGrabbed)
|
||||
{
|
||||
handDelta = lastPosition - grabHand.position; //The vector of hand movement between frames
|
||||
pivot.RotateAround(pivot.position, pivot.forward, 200 * (handDelta.y));
|
||||
|
||||
if(pivot.eulerAngles.z < minAngle)
|
||||
{
|
||||
pivot.eulerAngles = new Vector3(pivot.rotation.x, pivot.rotation.y, minAngle);
|
||||
if (!eventCalled)
|
||||
{
|
||||
onUp.Invoke();
|
||||
eventCalled = true;
|
||||
}
|
||||
}
|
||||
else if(pivot.eulerAngles.z > maxAngle)
|
||||
{
|
||||
pivot.eulerAngles = new Vector3(pivot.rotation.x, pivot.rotation.y, maxAngle);
|
||||
if (!eventCalled)
|
||||
{
|
||||
onDown.Invoke();
|
||||
eventCalled = true;
|
||||
}
|
||||
}
|
||||
|
||||
lastPosition = grabHand.position;
|
||||
}
|
||||
}
|
||||
|
||||
public void GrabLever(Transform hand)
|
||||
{
|
||||
leverGrabbed = !leverGrabbed;
|
||||
grabHand = hand;
|
||||
handDelta = Vector3.zero;
|
||||
lastPosition = grabHand.position;
|
||||
eventCalled = false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
11
Assets/Project Files/Scripts/JoonasP/LeverController.cs.meta
Normal file
11
Assets/Project Files/Scripts/JoonasP/LeverController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 090ddea5aafb4e945868d2613c3bfbaa
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
22
Assets/Project Files/Scripts/JoonasP/LeverEventTest.cs
Normal file
22
Assets/Project Files/Scripts/JoonasP/LeverEventTest.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class LeverEventTest : MonoBehaviour
|
||||
{
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
gameObject.GetComponent<Renderer>().material.color = Color.red;
|
||||
}
|
||||
|
||||
public void On()
|
||||
{
|
||||
gameObject.GetComponent<Renderer>().material.color = Color.green;
|
||||
}
|
||||
|
||||
public void Off()
|
||||
{
|
||||
gameObject.GetComponent<Renderer>().material.color = Color.red;
|
||||
}
|
||||
}
|
||||
11
Assets/Project Files/Scripts/JoonasP/LeverEventTest.cs.meta
Normal file
11
Assets/Project Files/Scripts/JoonasP/LeverEventTest.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a90699fc7b10474aaac51ec87819dc6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user