- Home /
Raycast missing collider target
i have a code to get some closest neighbour of node with waypoints and raycast. the "a" picture is the picture of waypoints location(the grey dot) that supposed to cast a raycast to each waypoints and the "b" is an image of raycast results, it's miss on several spot(red circled). it has at about 89 waypoints, and each waypoints location is right. this is a part of my code
for(flag = 0; flag< wayPoints.WPList.Length; flag++) // iteration to seek neighbour nodes
{
for (flagNeigh = 0; flagNeigh < wayPoints.WPList.Length; flagNeigh++)
{
if (wayPoints.WPList[flag].loc != wayPoints.WPList[flagNeigh].loc) // dump its own node location
{
if (Physics.Raycast(wayPoints.WPList[flag].loc.position, wayPoints.WPList[flagNeigh].loc.position, out hitted, Vector3.Distance(wayPoints.WPList[flag].loc.position, wayPoints.WPList[flagNeigh].loc.position))) // raycasting to each node else its self
{
Debug.DrawLine(wayPoints.WPList[flag].loc.position, hitted.collider.gameObject.transform.position);
}
}
}
}
Edit : X.X.loc is location which is in Transform.
i put this part of function in Start().
The y posisition of each waypoints is the same at 2, the x and z position difeerent as in the picture "a".
am i missing something ?
[a]. waypoints location
[b]. raycast results
[3]: /storage/temp/5480-b.jpg
Hi Bonifatius,
To be honest, seems like your rays are being fired from different locations if compared to the image [a]. Can you repeat this test with fewer points and check out if they're working as intended? If/When they're O$$anonymous$$, then you add the whole set.
@daniloshimura : i've tried to $$anonymous$$imize the second iteration to a round iteration, but still, one error happens, teh raycast supposed to cast a waypoint in the middle, but the resul it's cought at the left side
@whydoidoit no, no waypoints outside the terrain or behind the crossing object at the right side...
DrawLine isn't going to where the hit occurred (`hitted.point`) but ins$$anonymous$$d is going to the center position of the object hit. That's why all those lines go to the center of the red house, or to the center of the "border" colliders.
it's true, but i just try to show where i set the source and destination of the raycast, and draw line is just to show the results of my raycast..
Answer by Bonifatius GK · Dec 03, 2012 at 05:12 PM
not an answer
sorry to add some update on answer, because only 2 max attachment i can use. this is an image after i change the second iteration 1 time. it shown on the console that the waypoint 2 should use waypoint 38 as target, but the result, it cought in retrictor 2
the red lines is the raycast supposed to do, the white lines is the result of raycast.. thx
[1]: /storage/temp/5489-d.jpg
Hi Bonifatus,
Debug.DrawLine draws a line between point 1 and point 2. The Ray stores a point of origin and a direction. So, in this image"d", seems like your're drawing a line between the point and the direction vector, which should be something near Vector3.zero, which is the center of the world. If you're trying to draw a line using the Ray data, you'll want to do something like this:
Debug.DrawLine(ray.origin, (ray.origin + ray.direction) * distance);
Oh sorry, now I've read the post again and I misunderstood what you asked for. If you read your code you'll see that there's a problem with your ray cast.
Physics.Raycast(wayPoints.WPList[flag].loc.position, wayPoints.WPList[flagNeigh].loc.position)
The second argument is a direction, not a world position.
ah... I see, this is just my miss understanding to the function, thanks a lot..... cause it's said that the direction is Vector3, so i thought that it's the world location of the target..
Finally!! The first time the OP asked this question, they got the same answer "2nd input is a direction," but wasn't ready to understand it.
the direction (the second parameter) is (targetPosition - sourcePosition).normalized
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Physics.Raycast to Physics2D.Raycast? 1 Answer
C# raycast shooting script broken 1 Answer
Raycasting in 2D is not working 0 Answers