Skip to content

Commit df08540

Browse files
authored
Merge pull request #248 from HodgsonSDAS/BasicCursor-fixes
Basic cursor null ref and bitwise fixes
2 parents 0a94765 + c4c6cdf commit df08540

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

Assets/HoloToolkit/Input/Scripts/BasicCursor.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,11 @@ public struct RaycastResult
2626

2727
private MeshRenderer meshRenderer;
2828

29-
private bool hasLoggedGazeManagerError;
29+
private GazeManager gazeManager;
3030

3131
protected virtual void Awake()
3232
{
33-
if ((GazeManager.Instance.RaycastLayerMask & this.gameObject.layer) == 0)
34-
{
35-
Debug.LogError("The cursor has a layer that is checked in the GazeManager's Raycast Layer Mask. Change the cursor layer (e.g.: to Ignore Raycast) or uncheck the layer in GazeManager: " +
36-
LayerMask.LayerToName(this.gameObject.layer));
37-
}
38-
39-
meshRenderer = this.gameObject.GetComponent<MeshRenderer>();
33+
meshRenderer = gameObject.GetComponent<MeshRenderer>();
4034

4135
if (meshRenderer == null)
4236
{
@@ -48,21 +42,28 @@ protected virtual void Awake()
4842
meshRenderer.enabled = false;
4943

5044
// Cache the cursor default rotation so the cursor can be rotated with respect to the original orientation.
51-
cursorDefaultRotation = this.gameObject.transform.rotation;
45+
cursorDefaultRotation = gameObject.transform.rotation;
5246
}
5347

54-
protected virtual RaycastResult CalculateRayIntersect()
48+
protected virtual void Start()
5549
{
56-
RaycastResult result = new RaycastResult();
57-
if (GazeManager.Instance == null)
50+
gazeManager = GazeManager.Instance;
51+
52+
if (gazeManager == null)
5853
{
59-
if (!hasLoggedGazeManagerError)
60-
{
61-
Debug.LogError("Must have a GazeManager somewhere in the scene.");
62-
hasLoggedGazeManagerError = true;
63-
}
64-
return result;
54+
Debug.LogError("Must have a GazeManager somewhere in the scene.");
6555
}
56+
57+
if ((GazeManager.Instance.RaycastLayerMask & (1 << gameObject.layer)) != 0)
58+
{
59+
Debug.LogError("The cursor has a layer that is checked in the GazeManager's Raycast Layer Mask. Change the cursor layer (e.g.: to Ignore Raycast) or uncheck the layer in GazeManager: " +
60+
LayerMask.LayerToName(gameObject.layer));
61+
}
62+
}
63+
64+
protected virtual RaycastResult CalculateRayIntersect()
65+
{
66+
RaycastResult result = new RaycastResult();
6667
result.Hit = GazeManager.Instance.Hit;
6768
result.Position = GazeManager.Instance.Position;
6869
result.Normal = GazeManager.Instance.Normal;
@@ -71,7 +72,7 @@ protected virtual RaycastResult CalculateRayIntersect()
7172

7273
protected virtual void LateUpdate()
7374
{
74-
if (meshRenderer == null)
75+
if (meshRenderer == null || gazeManager == null)
7576
{
7677
return;
7778
}
@@ -83,11 +84,11 @@ protected virtual void LateUpdate()
8384
meshRenderer.enabled = rayResult.Hit;
8485

8586
// Place the cursor at the calculated position.
86-
this.gameObject.transform.position = rayResult.Position + rayResult.Normal * DistanceFromCollision;
87+
gameObject.transform.position = rayResult.Position + rayResult.Normal * DistanceFromCollision;
8788

8889
// Reorient the cursor to match the hit object normal.
89-
this.gameObject.transform.up = rayResult.Normal;
90-
this.gameObject.transform.rotation *= cursorDefaultRotation;
90+
gameObject.transform.up = rayResult.Normal;
91+
gameObject.transform.rotation *= cursorDefaultRotation;
9192
}
9293
}
9394
}

0 commit comments

Comments
 (0)