- Home /
Ray search
Hello first time here. Here's my problem :
I'm working on 2D (X,Z) Y is constant at 60
I want to make sure that an intersection can "search" around for a Road (these are simple cubes)
These cubes have scripts attached to em (Road,Intersection)
So, in my script, I have this :
#Intersection#
//posXZ is a table that contains the coords of the intersection (X,Z)
vectu = new Vector3(posXZ[0]-2,60,posXZ[1]);
ray = new Ray(vectu,vectu - new Vector3(0,60,0));
RaycastHit hit ;
if (Physics.Raycast(ray,out hit))
{
//DO SOMETHING
}
My problem is that, i don't know how to use properly the direction mentionned in the manual for RAY. I know that i have to create a total of 4 rays which are gonna check in a range of 2 units if there's a cube with the tag "road" but i don't really know how to use the direction.
Can somebody explain with a little diagram please ?
Answer by lighting · Jun 11, 2013 at 07:22 AM
I think, that Scripting Reference with description of Ray() constructor is very helpful. There is example: public Ray ray = new Ray(transform.position, transform.forward);
In your case the solution could be (depending on how you have the coordinates setup):
rayUp = new Ray(transform.position, Vector3.up);
rayDown = new Ray(transform.position, Vector3.down);
rayLeft = new Ray(transform.position, Vector3.left);
rayRight = new Ray(transform.position, Vector3.right);
Oh god....I'm so dumb! Didn't saw these Vectors -_-' Thanks ! The Vectors left,right,forward,back are using (0, 0, 1) as coords, will it take the first object that it encounters? Or the last ? (for example, if there's something behind the road) ?
Answer by NuclearBUSE · Jun 11, 2013 at 09:27 AM
Yeah, haven't seen these functions... :( It's working anyway, thanks !
Your answer
Follow this Question
Related Questions
is it possible to have raycasts to cover the whole screen in one Update() time? 0 Answers
SORPG: Distance problem? 1 Answer
Confused as to how my RaycastHit script works 1 Answer
Shooting with touch 1 Answer
Ray direction, projection matrix 0 Answers