Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Can someone explain what the "point" on RaycastHit.point is?

Discussion in 'Scripting' started by JBEDUnity, Jul 31, 2020.

  1. JBEDUnity

    JBEDUnity

    Joined:
    Jul 13, 2020
    Posts:
    23
    Every time I read RaycastHit.point.x, y, or z I get values that make no sense. I'm trying to make heads or tails of what point I'm reading. I though it was the point the ray hit the mesh collider but the numbers I get are way off.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    7,973
    It's exactly that. What doesn't make sense about the numbers?

    Try setting some object's transform.position to hit.point, and you should see it follow your cursor exactly.

    I suspect one of two things is happening:
    1) you may be expecting the point to be in local space to the collider or something to that effect? hit.point is always in world space.
    2) you may have an invisible (trigger?) collider blocking your raycast
     
    Bunny83 likes this.
  3. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    3,794
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,130
    It can help to also print
    raycastHit.transform.name
    , in case you missed and hit a wall or something. Another trick is you can create a Cube while in Play mode (iwhich is auto-destroyed when you stop) and move it to that x,y,z. You may notice that it's directly on some wall, or barely on the edge of a pillar. Another crude trick is "square selecting" around the hit, which might highlight an invisible collider you didn't realize was there.
     
    Bunny83 likes this.
  5. JBEDUnity

    JBEDUnity

    Joined:
    Jul 13, 2020
    Posts:
    23

    Thanks, tried that. It keeps hitting CustomRightHand (where the script is attached) and not the ray out. That explains the weird values. Don't know how to fix it but at least I've got an idea what's going on.
     
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,130
    Raycasts have one odd rule that works in your favor -- they don't hit things that they start inside. Normally that means the best way to shoot a power bolt from your hand is to raycast from the center. Your hand can never lbock it. I'm not sure if that also works for concave colliders (where a ray could leave your palm but hit a finger). The problem might be that the origin is the end of the arm, just before the hand collider starts?

    If I'm serious about tracking these down, I'll make a red ball and add
    redBall.position=ray1.start;
    . and a green ball at the hit point, and the drawRay for good measure. It's a variant of the old "when things aren't working, something has the wrong value, so print _everything_".
     
unityunity