- Home /
Get the distance between the origin and HitPoint for Raycast?
Hey guys, I need a raycast to check the distance between two points for an "if" statement. So as an example:
if("Distance of Ray" > 10)
{
//Do something
}
else
{
//Do something else
}
Thanks in advance!
Comment
Best Answer
Answer by GameVortex · Nov 29, 2013 at 12:18 PM
To check the distance between the two points you can use: Vector3.Distance(origin, hitpoint). Here is the script reference on it: Vector3.Distance
Hmmm...yes but how could I check if the distance is bigger than another distance (that I would like to set myself). Would you know how to do that? Sorry, I'm not that great with Vectors. At the moment I have:
Vector3 dis = Vector3.Distance(Hit.point, FirePoint.position);
Vector3.Distance returns the distance as a float, not a Vector3. Just change the varibale to float and insert it into your if test like this:
float dis = Vector3.Distance(Hit.point, FirePoint.position);
if(dis > 10)
{
//Do something
}
else
{
//Do something else
}
Oh I just realised it returns a float haha thank you very much!