[Solved] How to compare RaycastHit with coordinates?

Hi,

Lets say de Debug.Log shows me that RaycastHit = (0.8, 1.5, -0.5) which (if I’m right) means that those are the x, y and z coordinates of the hit.

Now I want something like this:

if (RaycastHit     x>0.5f && <3.5f,
                y>0.0f && <3.0f)
    {
        Do This...
    }
if (RaycastHit     x>2.5f && <5.5f,
                y>2.0f && <5.0f)
    {
        Do That...
    }

I know this isn’t proper code but hopefully I made it clear what my intentions are.

RaycastHit.point

Thank you for taking the time to place that link. I already found it before posting this question, but I can’t find the answer I’m looking for in it.

RaycastHit.point is the vector3 that represents the collision point in world space… you want to compare the x/y/z coordinates of the hit point with some values… … :eyes:

edit: if you’re asking “how do I compare a vector3 against some values?”

// generic answer
if(myVector3.x < 10f) { ...

// bit more specific
if(hit.point.x < 10f) { ...

If I understood your problem correctly, something like this:

RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);
if (hit.point.x>0.5f && hit.point.<3.5f && hit.point.y>0.0f && hit.point.y<3.0f)
    {
       // Do This...
    }
if (hit.point.x>2.5f && hit.point.x<5.5f && hit.point.y>2.0f && hit.point.y<5.0f)
    {
       // Do That...
    }

ps.: code is untested

That’s not a very good idea - if the raycast doesn’t hit something, hit.point will be 0,0,0. You probably want to handle the case “The raycast hit nothing” differently from “the raycast hit something at near (0,0,0)”

You guys made a noob very happy. Thank you!