Documentation for the Unity C# Library
Loading...
Searching...
No Matches
LaserMovable.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using Autohand;
5using Autohand.Demo;
6using PixoVR.Core;
7using PixoVR.Event;
8using Sirenix.OdinInspector;
9using UnityEngine;
10
12{
13 None,
14 Initial,
17}
18
19[RequireComponent(typeof(DistanceGrabbable))]
20public class LaserMovable : MonoBehaviour
21{
22 [SerializeField] private Transform _transformToMove;
23 [SerializeField] private DistanceGrabbable _distanceGrabbable;
24 [SerializeField] private RotationOnRelease _rotationOnRelease = RotationOnRelease.Initial;
25 [SerializeField] private float _zoomInSpeed = 2;
26 [SerializeField] private bool _invertedForward = true;
27
28 private const float MAX_DISTANCE = 15;
29 private const float MIN_DISTANCE = 1;
30
31 private Hand _currentSelectingHand;
32 public bool IsPulling { get; internal set; }
33 public bool IsTargeted { get; internal set; }
34 private Transform _previousParent = null;
35 private Quaternion _rotationOnGrab;
36 private Vector3 _initialPosition;
37 private Quaternion _intitialRotation;
39
40 private bool _startedSelecting = false;
42 public virtual void Awake()
43 {
47 }
49 private void OnEnable()
50 {
51 _distanceGrabbable.StartTargeting.AddListener(OnStartTargeting);
52 _distanceGrabbable.StopTargeting.AddListener(OnStopTargeting);
53 _distanceGrabbable.StartSelecting.AddListener((Hand hand, Grabbable grabbable) =>
54 {
56 });
57 _distanceGrabbable.StopSelecting.AddListener((Hand hand, Grabbable grabbable) =>
58 {
59 _startedSelecting = false;
60 });
61 _inputListener.TriggerPressed += OnTriggerPressed;
62 _inputListener.TriggerReleased += OnTriggerReleased;
63
64 EventBetter.Listen(this, (WaypointRespawnEvent respawnEvent) =>
65 {
66 if (respawnEvent.RespawnTransform == _transformToMove)
67 {
68 _initialPosition = _transformToMove.position;
69 _intitialRotation = _transformToMove.rotation;
70 }
71 });
72
73 EventBetter.Listen(this, (LongPressEvent longPressEvent) =>
74 {
75 if (longPressEvent.Button != CommonButton.gripButton)
76 {
77 return;
78 }
79
80 if (_currentSelectingHand != null)
81 {
83 }
84
85 _transformToMove.position = _initialPosition;
86 _transformToMove.rotation = _intitialRotation;
87 });
88 }
89
90 private void OnDisable()
91 {
92 EventBetter.Unlisten<LongPressEvent>(this);
93 EventBetter.Unlisten<WaypointRespawnEvent>(this);
94 _distanceGrabbable.StartTargeting.RemoveListener(OnStartTargeting);
95 _distanceGrabbable.StopTargeting.RemoveListener(OnStopTargeting);
96 _inputListener.TriggerPressed -= OnTriggerPressed;
97 _inputListener.TriggerReleased -= OnTriggerReleased;
98
99 if (_currentSelectingHand != null)
100 {
102 }
103
104 IsPulling = false;
105 IsTargeted = false;
107 }
108
109 private void Update()
110 {
111 if (!IsTargeted)
112 {
113 return;
114 }
115
116 if (IsPulling)
117 {
118 float yAxis = GetCurrentHandYAxis();
119 if (yAxis != 0)
120 {
121 MoveAlongRay(yAxis);
122 }
123 }
125
126 private void MoveAlongRay(float moveDirection)
127 {
128 float distance = Vector3.Distance(_currentSelectingHand.transform.position, _transformToMove.position);
129 float deltaMove = -moveDirection * Time.deltaTime * _zoomInSpeed;
130
131 if (distance >= MAX_DISTANCE && deltaMove < 0)
132 {
133 return;
134 }
135
136 if (distance <= MIN_DISTANCE && deltaMove > 0)
137 {
138 return;
139 }
140
141 _transformToMove.Translate((_currentSelectingHand.transform.position - _transformToMove.position) * deltaMove,
142 Space.World);
143 }
144
145
146 private float GetCurrentHandYAxis()
147 {
149 }
150
151
152 private void OnStartTargeting(Hand hand, Grabbable grabbable)
153 {
154 if (hand.holdingObj != null || IsPulling)
155 {
156 return;
157 }
159 IsTargeted = true;
162
163 public virtual void StartTargeting()
164 {
165 }
166
167 private void OnStopTargeting(Hand hand, Grabbable grabbable)
168 {
170 {
171 return;
172 }
174 IsTargeted = false;
176 }
177
178 public virtual void StopTargeting()
179 {
180 }
181
182 public virtual void OnTriggerGrab(Hand hand, Grabbable grabbable)
183 {
184 if (hand.holdingObj != null)
185 {
186 return;
187 }
188 IsPulling = true;
191 _transformToMove.SetParent(_currentSelectingHand.transform);
192 }
194 public virtual void OnTriggerReleased(Hand hand, Grabbable grabbable)
195 {
196 IsPulling = false;
197 OnStopTargeting(hand, grabbable);
200 {
201 _transformToMove.rotation = _intitialRotation;
202 }
203 else if (_rotationOnRelease == RotationOnRelease.OnGrabStart)
204 {
205 _transformToMove.rotation = _rotationOnGrab;
206 }
207 else if (_rotationOnRelease == RotationOnRelease.LookAtCamera)
208 {
209 if (Camera.main != null)
210 {
211 Vector3 lookPosition = _transformToMove.position - Camera.main.transform.position;
212 //don't change the up vector
213 lookPosition.y = 0;
215 {
216 _transformToMove.rotation = Quaternion.LookRotation(lookPosition);
217 }
218 else
219 {
220 _transformToMove.rotation = Quaternion.LookRotation(Vector3.Scale(lookPosition, new Vector3(-1, -1, -1)));
221 }
222 }
223 }
224 }
225
226
227 private void OnTriggerPressed(Hand hand)
228 {
229 if (hand == _currentSelectingHand && IsTargeted)
230 {
232 }
233 }
234
235 private void OnTriggerReleased(Hand hand)
236 {
237 if (hand == _currentSelectingHand && IsTargeted)
238 {
240 }
241 }
RotationOnRelease
Vector2 GetAxis(Hand hand)
float GetCurrentHandYAxis()
Hand _currentSelectingHand
Quaternion _intitialRotation
void OnStartTargeting(Hand hand, Grabbable grabbable)
Quaternion _rotationOnGrab
bool _startedSelecting
Transform _previousParent
virtual void OnTriggerGrab(Hand hand, Grabbable grabbable)
void OnTriggerPressed(Hand hand)
void OnEnable()
const float MAX_DISTANCE
Transform _transformToMove
[SerializeField]
void OnStopTargeting(Hand hand, Grabbable grabbable)
void OnTriggerReleased(Hand hand)
DistanceGrabbable _distanceGrabbable
[SerializeField]
RotationOnRelease _rotationOnRelease
[SerializeField]
InputListener _inputListener
bool _invertedForward
[SerializeField]
const float MIN_DISTANCE
Vector3 _initialPosition
virtual void OnTriggerReleased(Hand hand, Grabbable grabbable)
virtual void Awake()
float _zoomInSpeed
[SerializeField]
virtual void StopTargeting()
virtual void StartTargeting()
void MoveAlongRay(float moveDirection)
Intentionally made partial, in case you want to extend it easily.
Unity components communication service.