Raycast and Linecast

Can anyone tell me what’s the different between raycast and linecast?

2 Answers

2

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.

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.