forked from KyleTheScientist/LightsCameraAction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepositionListener.cs
109 lines (95 loc) · 4.37 KB
/
RepositionListener.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using GorillaLocomotion;
using GorillaScience.Behaviors;
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace LightsCameraAction
{
public class RepositionListener : MonoBehaviour
{
Repositionable moveTarget, rotateTarget;
Transform activeHand;
public float followRate = 40f;
void Start()
{
Plugin.log.Debug("Start");
Plugin.Instance.inputTracker.leftGrip.OnPressed += LeftTryMoving;
Plugin.Instance.inputTracker.leftTrigger.OnPressed += LeftTryRotating;
Plugin.Instance.inputTracker.rightGrip.OnPressed += RightTryMoving;
Plugin.Instance.inputTracker.rightTrigger.OnPressed += RightTryRotating;
Plugin.Instance.inputTracker.leftGrip.OnReleased += LeftClearMoving;
Plugin.Instance.inputTracker.leftTrigger.OnReleased += LeftClearRotating;
Plugin.Instance.inputTracker.rightGrip.OnReleased += RightClearMoving;
Plugin.Instance.inputTracker.rightTrigger.OnReleased += RightClearRotating;
Plugin.log.Debug("End");
}
void LeftTryMoving() { TryMoving(true); }
void RightTryMoving() { TryMoving(false); }
void LeftTryRotating() { TryRotating(true); }
void RightTryRotating() { TryRotating(false); }
void LeftClearMoving() { moveTarget = null; }
void RightClearMoving() { moveTarget = null; }
void LeftClearRotating() { rotateTarget = null; }
void RightClearRotating() { rotateTarget = null; }
void TryMoving(bool left)
{
var target = FindClosestRepositionable(left);
if (!target) return;
moveTarget = target;
if (rotateTarget != moveTarget)
rotateTarget = null;
activeHand = left ? Player.Instance.leftControllerTransform : Player.Instance.rightControllerTransform;
}
void TryRotating(bool left)
{
var target = FindClosestRepositionable(left);
if (!target) return;
rotateTarget = target;
if (moveTarget != rotateTarget)
moveTarget = null;
activeHand = left ? Player.Instance.leftControllerTransform : Player.Instance.rightControllerTransform;
}
void FixedUpdate()
{
if (moveTarget && moveTarget.canMove)
{
moveTarget.transform.position = Vector3.Lerp(
moveTarget.transform.position,
activeHand.position + Vector3.up * .1f * Player.Instance.scale,
followRate * Time.deltaTime
);
}
if (rotateTarget && rotateTarget.canRotate)
{
Vector3 currentEuler = rotateTarget.transform.rotation.eulerAngles;
Vector3 targetEuler = activeHand.rotation.eulerAngles;
currentEuler.x = Mathf.LerpAngle(currentEuler.x, targetEuler.x, Time.deltaTime * followRate);
currentEuler.y = Mathf.LerpAngle(currentEuler.y, targetEuler.y, Time.deltaTime * followRate);
currentEuler.z = 0;
rotateTarget.transform.rotation = Quaternion.Euler(currentEuler);
}
}
public Repositionable FindClosestRepositionable(bool left)
{
Repositionable closestObject = null;
float closestDistance = Mathf.Infinity;
Transform playerHand = left ? Player.Instance.leftControllerTransform : Player.Instance.rightControllerTransform;
// Iterate through each object to find the closest one
foreach (Repositionable repositionableObject in FindObjectsOfType<Repositionable>())
{
float distanceToPlayerHand = Vector3.Distance(
playerHand.position,
repositionableObject.transform.position - repositionableObject.transform.forward * .1f * Player.Instance.scale
);
if (distanceToPlayerHand < closestDistance && distanceToPlayerHand < .2f * Player.Instance.scale)
{
closestDistance = distanceToPlayerHand;
closestObject = repositionableObject;
}
}
return closestObject;
}
}
public class Repositionable : MonoBehaviour { public bool canRotate = true, canMove = true; }
}