14 [Tooltip(
"The Cursor object the direction indicator will be positioned around.")]
17 [Tooltip(
"Model to display the direction to the object this script is attached to.")]
20 [Tooltip(
"Color to shade the direction indicator.")]
21 public Color DirectionIndicatorColor = Color.blue;
23 [Tooltip(
"Allowable percentage inside the holographic frame to continue to show a directional indicator.")]
25 public float VisibilitySafeFactor = 0.1f;
27 [Tooltip(
"Multiplier to decrease the distance from the cursor center an object is rendered to keep it in view.")]
29 public float MetersFromCursor = 0.3f;
32 private Quaternion directionIndicatorDefaultRotation = Quaternion.identity;
35 private Renderer directionIndicatorRenderer;
38 private Material indicatorMaterial;
41 private bool isDirectionIndicatorVisible;
47 Debug.LogError(
"Please include a GameObject for the cursor.");
50 if (DirectionIndicatorObject == null)
52 Debug.LogError(
"Please include a GameObject for the Direction Indicator.");
56 DirectionIndicatorObject = InstantiateDirectionIndicator(DirectionIndicatorObject);
58 if (DirectionIndicatorObject == null)
60 Debug.LogError(
"Direction Indicator failed to instantiate.");
66 DestroyImmediate(indicatorMaterial);
67 Destroy(DirectionIndicatorObject);
70 private GameObject InstantiateDirectionIndicator(GameObject directionIndicator)
72 if (directionIndicator == null)
77 GameObject indicator = Instantiate(directionIndicator);
80 directionIndicatorDefaultRotation = indicator.transform.rotation;
81 directionIndicatorRenderer = indicator.GetComponent<Renderer>();
84 directionIndicatorRenderer.enabled =
false;
87 foreach (Collider indicatorCollider
in indicator.GetComponents<Collider>())
89 Destroy(indicatorCollider);
92 foreach (Rigidbody rigidBody
in indicator.GetComponents<Rigidbody>())
97 indicatorMaterial = directionIndicatorRenderer.material;
98 indicatorMaterial.color = DirectionIndicatorColor;
99 indicatorMaterial.SetColor(
"_TintColor", DirectionIndicatorColor);
106 if (DirectionIndicatorObject == null)
112 Vector3 camToObjectDirection = gameObject.transform.position - mainCamera.transform.position;
113 camToObjectDirection.Normalize();
116 isDirectionIndicatorVisible = !IsTargetVisible(mainCamera);
117 directionIndicatorRenderer.enabled = isDirectionIndicatorVisible;
119 if (isDirectionIndicatorVisible)
123 GetDirectionIndicatorPositionAndRotation(
124 camToObjectDirection,
125 mainCamera.transform,
129 DirectionIndicatorObject.transform.position = position;
130 DirectionIndicatorObject.transform.rotation = rotation;
134 private bool IsTargetVisible(Camera mainCamera)
137 Vector3 targetViewportPosition = mainCamera.WorldToViewportPoint(gameObject.transform.position);
138 return (targetViewportPosition.x > VisibilitySafeFactor && targetViewportPosition.x < 1 - VisibilitySafeFactor &&
139 targetViewportPosition.y > VisibilitySafeFactor && targetViewportPosition.y < 1 - VisibilitySafeFactor &&
140 targetViewportPosition.z > 0);
143 private void GetDirectionIndicatorPositionAndRotation(Vector3 camToObjectDirection, Transform cameraTransform, out Vector3 position, out Quaternion rotation)
147 Vector3 origin = Cursor.transform.position;
149 Vector3 cursorIndicatorDirection = Vector3.ProjectOnPlane(camToObjectDirection, -1 * cameraTransform.forward);
150 cursorIndicatorDirection.Normalize();
154 if (cursorIndicatorDirection == Vector3.zero)
156 cursorIndicatorDirection = cameraTransform.right;
160 position = origin + cursorIndicatorDirection * MetersFromCursor;
163 rotation = Quaternion.LookRotation(cameraTransform.forward, cursorIndicatorDirection) * directionIndicatorDefaultRotation;