- Home /
Difference between Linecast and Raycast
Hi,
I've read the documentation page but could not get the hold of their difference.
Thanks
Answer by DigiScot · Jul 13, 2015 at 09:54 AM
The main difference is a raycast you set the start point then the direction and the distance to check in that direction, a linecast you simply set a start and end and it checks between those 2 points.
So if you know the end destination specifically, use linecast, if you want to check in a specific direction but have no specific end point, use raycast.
Otherwise, cost is pretty much the same, it all depends how far it's checking and what it's checking against.
correct, i'm not sure what Gruffy ment in his post. Also he confuses Raycast and Linecast several times in his post.
They are equally expensive. A Raycast "allows" to cast an infinite ray, but that fact on it's own doesn't make it more expensive. Unity's physics system most likely uses some kind of Octree to deter$$anonymous$$e which colliders it has to check.
Guys, apologies, my explanation was a bit vague at the time unfortunately, and I had indeed confused the two in my explanation. Don`t know why, I suspect its something to do with me being stupid!
Though I will say what DigiScot had eloquently put, was the intention of my comment. I do have to say, the way I said it was convoluted slightly, perhaps a little confusing and very badly worded lol
Soz guys, hope I`m forgiven for it :) Gruffy
Answer by Gruffy · Dec 06, 2014 at 02:36 PM
Line cast is effectively cheaper than a ray cast. thought they are very close in speed really. The ray cast simply allows us to also return a distance/ cut off point. A line cast is infinite effectively, if you read between those two sentences, you could see how a ray cast could be made more efficient than the linecast as the need to check an infinite linear distance may not be required. For another nice efficient use of casting thought, check out Physics.Spherecast..... I will leave that with you Click me
Take care bud, hope that helps clarify at least something for you there. (no doubt this statement may raise purist arguments over the whole line vs ray thing lol) Gruffy
Edit: To add its also probably best to include the fact you could also specify things to ignore with ray casts, which I don`t think is possible with a line cast.
Linecast() has 5 overloads. Two of which include: public static bool Linecast(Vector3 start, Vector3 end, int layer$$anonymous$$ask); public static bool Linecast(Vector3 start, Vector3 end, out RaycastHit hitInfo, int layer$$anonymous$$ask);
Both of these overload options give you the ability to ignore specified objects by layers with a layer$$anonymous$$ask.
Your answer
Follow this Question
Related Questions
Springjoint transmit wrong forces 0 Answers
mecanim and rigidbody gravity effect doesnt work 1 Answer
Problem with moving an object and Uni2D Colliders 0 Answers
How to draw line in the game, not editor. 2 Answers
Change the "down" axis for 2D 2 Answers