- Home /
Raycast and Linecast
Can anyone tell me what's the different between raycast and linecast?
Answer by Kacer · Sep 08, 2011 at 09:18 AM
Raycast can be used to get information from objects you're casting the ray at, Linecast returns true if there is a collider between the start and the endpoint of the line.
If i understand the documentation right, then you cant chose exactly where the linerender ends, you just say it should go from "a" to "b", whereas the raycast casts a ray from a point in a direction of your choosing.
Personally i'd use a linecast for tripwires and the like in games, and a raycast for shooting at stuff.
Hope this helps
Kacer
Don't know at the time this answer was posted but it might not be actual anymore: - Linecast can have a RaycastHit out parameter to provide more detailed data on the collision, same as Raycast. - Raycast has a start point, a direction (whether entered as is as parameters or through a Ray), and can have a distance. Therefore it can achieve quite the same thing as Linecast in term of space traveled. I tend to believe the main difference btw Raycast and Linecast is what bad3p says below.
According to the documentation, Physics.Linecast still returns a bool -- true if it hit a collider. However, Physics2D.Linecast returns a RaycastHit2D with information on the object hit.
Answer by bad3p · Sep 12, 2012 at 09:53 PM
Linecast will detect intersection even if both StartPos and EndPos are within the collider. Raycast will not detect intersection if its Origin is within the collider.
Linecast is slower but safer way to detect intersections. Raycast is faster but not accurate.
Actually linecast is not slower according to http://forum.unity3d.com/threads/linecast-vs-raycast-float-to-int-fixedupdate.2483/
@bad3p I know this is a old post but when reading your comment "Linecast will detect intersection even if both StartPos and EndPos are within the collider" I thought I'd use line cast over raycast.
I generate a flight path and every 0.2 seconds I call a routine that sends out line casts in a 180 degree arc looking for colliders, all works fine until one of the line cast arcs spawns say inside a block, like a tower block, even with the ends of the line cast showing through the block they do not show a detection, has line cast been changed, now on Unity 5.5
Cheers.
I have also encountered your case where Linecast goes through the collider and detects no collision. This happens when Linecast's startpoint is inside the collider while the endpoint is outside of it. When you switch the two points to its opposite places, the Linecast detects a collision. So given this scenario the solution should be quite obvious, Linecast still requires some sort of direction. In your case, your Linecast's startpoint started inside the tower block then goes through it until it reaches its endpoint. If you want a collision to take place in such case, always put the startpoint outside of the collider.