[RESOLVED] Raycast - Calculate Angular Difference from Object

Hi. I’m a spatial memory researcher using Unity to develop a VR experiment. In the experiment, participants will navigate a maze populated with objects at random locations. I will then open an identical environment with the objects ‘removed’ (actually invisible), and ask them to point with the mouse reticle to a specific object; in some cases the object may be occluded by a wall (see attached ‘Raycast Help’).

From what I gather, there is a way to assign objects to a layer so that the raycast will ignore walls. This is great, but…

My question: Is it possible to calculate the angular difference between the ray vector and the centroid of a target object?

It is unlikely participants will aim the raycast directly on the target object, however, I still must take a measurement of their attempt, and record the angular difference between their current location and a target object.

I have some experience in Unity, but my scripting skills are not robust. I appreciate any help the community can offer. Thank you!!

@buttmatrix I think you can do it like this:

 public GameObject player;
 public GameObject target;
 public Vector3 tLoc;
 public float angle;
 public Vector3 playerLoc;
 
 void Update(){
 tLoc = target.transform.position - this.transform.position;
 tLoc.y = 0;
 playerLoc = player.transform.forward;
 playerLoc.y = 0
 angle = Vector3.Angle(playerLoc, tLoc );
 Debug.Log(angle);
 }

(using @BlackWingsCorp base code)

Hey, one very easy way to calculate the angle using raycast is to use Vector3.Angle(), it will return the angle between 2 objects whether constants or moving.

Here’s a C# example:

public GameObject player;
public GameObject target;
public Vector3 tLoc;
public float angle;


void Update(){
tLoc = target.transform.position - this.transform.position
angle = Vector3.Angle(player.transform.forward, tLoc );
Debug.Log(angle);
}

Hope this helps

Based on the initial sample above…
To ignore the y position you can either use Vector2 functions…
Or you can use Vector3 and set the y value to zero…
First I’ll show you the Vector3 way…

public GameObject player;
public GameObject target;
public Vector3 pLoc;
public Vector3 tLoc;
public float angle;
void Update(){
tLoc = target.transform.position - this.transform.position
pLoc = player.transform.forward;
tLoc.y = 0.0f;
pLoc.y = 0.0f;
angle = Vector3.Angle(pLoc, tLoc );
Debug.Log(angle);
}

And then this is Vector2 way… they are similar in function, but I think the Vector2 way adds unnecessary conversions.

public GameObject player;
public GameObject target;
public Vector3 vector;
public Vector2 pLoc;
public Vector2 tLoc;
public float angle;
void Update(){
vector = target.transform.position - this.transform.position;
tLoc = new Vector2(vector.x, vector.z); // We are replacing the Y value with Z 
pLoc = new Vector2(player.transform.forward.x, player.transform.forward.z);
angle = Vector2.Angle(pLoc, tLoc );
Debug.Log(angle);
}

Please take care that I have “winged” these script changes and they haven’t been tested.