- Home /
Raycasting To Objects
I have this array of all objects in the world(GameObject[] allObjects), what I need to do is fire a ray at each object from my players position in order to see if said object is behind another one. the origin is simple, its just transform.position(script attached to player cam), but the rotation is very difficult for me to get my head around.
Thanks for the help in advance.
Answer by Habitablaba · Oct 29, 2014 at 07:52 PM
By rotation, I'm assuming you mean direction to the other object?
This is a simple thing to find. Direction vectors can be calculated by taking the 'from' position, and subtracting the 'to' position. So for each object in your array:
var directionVector = transform.position - other.transform.position;
Will that return the angle that can be used as the second parameter in the physics.raycast?
I'm not seeing a version of Physics.Raycast that takes an angle. I see one that takes an origin Vector3 and a direction Vector3, though. The above 'directionVector' would fit into that.
Ahh yeh, forgot it was a Vector and not an angle, thanks for the answer. I will post here if I have any more questions.
Just further explaining what Habitablaba said:
If A and C are the positions of your “from” and “to“ objects (a position is a vector), then B = C-A is the direction vector.
Your answer
Follow this Question
Related Questions
Having Trouble with Vector3.Angle 1 Answer
C# Check Physics.Raycast Once 0 Answers
Problem with raycast 2 Answers
Raycast not rotating with object it casts from 0 Answers
Getting the rotation of an object does not return correct rotation 1 Answer