Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Solved] How to compare RaycastHit with coordinates?

Discussion in 'Scripting' started by DarkTulip, May 26, 2016.

  1. DarkTulip

    DarkTulip

    Joined:
    Oct 21, 2014
    Posts:
    74
    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:
    Code (CSharp):
    1. if (RaycastHit     x>0.5f && <3.5f,
    2.                 y>0.0f && <3.0f)
    3.     {
    4.         Do This...
    5.     }
    6. if (RaycastHit     x>2.5f && <5.5f,
    7.                 y>2.0f && <5.0f)
    8.     {
    9.         Do That...
    10.     }
    11.  
    I know this isn't proper code but hopefully I made it clear what my intentions are.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. DarkTulip

    DarkTulip

    Joined:
    Oct 21, 2014
    Posts:
    74
    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.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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... ... o_O


    edit: if you're asking "how do I compare a vector3 against some values?"

    Code (csharp):
    1.  
    2. // generic answer
    3. if(myVector3.x < 10f) { ...
    4.  
    5. // bit more specific
    6. if(hit.point.x < 10f) { ...
    7.  
     
    Last edited: May 26, 2016
  5. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    If I understood your problem correctly, something like this:
    Code (CSharp):
    1. RaycastHit hit;
    2. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    3. Physics.Raycast(ray, out hit);
    4. if (hit.point.x>0.5f && hit.point.<3.5f && hit.point.y>0.0f && hit.point.y<3.0f)
    5.     {
    6.        // Do This...
    7.     }
    8. if (hit.point.x>2.5f && hit.point.x<5.5f && hit.point.y>2.0f && hit.point.y<5.0f)
    9.     {
    10.        // Do That...
    11.     }
    ps.: code is untested
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    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)"
     
  7. DarkTulip

    DarkTulip

    Joined:
    Oct 21, 2014
    Posts:
    74
    You guys made a noob very happy. Thank you!