13 public float ControllerReturnFactor = 0.25f;
14 public float ControllerTimeBeforeReturn = 0.5f;
16 [Tooltip(
"Use unscaled time. This is useful for games that have a pause mechanism or otherwise adjust the game timescale.")]
17 public bool UseUnscaledTime =
true;
37 public bool VisualizeController =
true;
45 public bool ControllerInView {
get;
private set; }
49 private Vector3 localPosition;
50 private Vector3 localRotation;
52 private Renderer visualRenderer;
53 private MaterialPropertyBlock visualPropertyBlock;
54 private int mainTexId;
56 private float timeBeforeReturn;
60 if (!Application.isEditor)
65 mainTexId = Shader.PropertyToID(
"_MainTex");
67 ControllerSourceState.
Pressed =
false;
68 ControllerSourceState.
Grasped =
false;
73 IsPositionAvailable =
false,
74 IsRotationAvailable =
false 77 localPosition = ControllerVisualizer.transform.position;
78 InitialPosition = localPosition;
86 visualRenderer = ControllerVisualizer.GetComponent<Renderer>();
87 visualPropertyBlock =
new MaterialPropertyBlock();
88 visualRenderer.SetPropertyBlock(visualPropertyBlock);
93 UpdateControllerVisualization();
95 float deltaTime = UseUnscaledTime ? Time.unscaledDeltaTime : Time.deltaTime;
97 float smoothingFactor = deltaTime * 30.0f * ControllerReturnFactor;
99 if (timeBeforeReturn > 0.0f)
101 timeBeforeReturn = Mathf.Clamp(timeBeforeReturn - deltaTime, 0.0f, ControllerTimeBeforeReturn);
107 if (MenuButtonControl)
117 if (ControllerSourceState.
Pressed)
119 timeBeforeReturn = ControllerTimeBeforeReturn;
122 if (timeBeforeReturn <= 0.0f)
124 localPosition = Vector3.Slerp(localPosition, InitialPosition, smoothingFactor);
125 if (localPosition == InitialPosition)
127 ControllerInView =
false;
131 Vector3 translate = Vector3.zero;
133 if (PrimaryAxisTranslateControl && SecondaryAxisTranslateControl)
141 if (PrimaryAxisRotateControl && SecondaryAxisRotateControl && TertiaryAxisRotateControl)
152 localRotation += rotate;
157 bool controllerTranslateActive =
165 if (controllerTranslateActive ||
168 ControllerSourceState.
Grasped ||
171 timeBeforeReturn = ControllerTimeBeforeReturn;
172 ControllerInView =
true;
175 localPosition += translate;
180 ControllerVisualizer.transform.forward =
CameraCache.
Main.transform.forward;
182 ControllerVisualizer.transform.Rotate(localRotation);
186 visualPropertyBlock.SetTexture(mainTexId, ControllerSourceState.
Pressed ? HandDownTexture : HandUpTexture);
187 visualRenderer.SetPropertyBlock(visualPropertyBlock);
194 Vector3 up = ControllerVisualizer.transform.TransformDirection(Vector3.up);
195 ControllerSourceState.
SourcePose.
PointerRay =
new Ray(ControllerVisualizer.transform.position, up);
200 if (ShowPointingRay && Physics.Raycast(newRay))
203 Debug.DrawRay(newRay.origin, newRay.direction, Color.cyan);
213 private void UpdateControllerVisualization()
215 visualRenderer.material.SetColor(
"_Color", ControllerInView ? ActiveControllerColor : DroppedControllerColor);
217 if (ControllerVisualizer.activeSelf != VisualizeController)
219 ControllerVisualizer.SetActive(VisualizeController);