Search Unity

Resolved RaycastHit.point not correctly matching the world space

Discussion in 'Physics' started by FedoraKirb, Jun 3, 2023.

  1. FedoraKirb

    FedoraKirb

    Joined:
    Apr 20, 2021
    Posts:
    6
    I'm trying to determine where on the ground the mouse is hovering over, so I'm using a raycast and hit.point to draw a ray to the ground.
    Code (CSharp):
    1. [SerializeField] protected Camera cam;
    2. protected Vector3 target;
    3. ...
    4. void FixedUpdate()
    5. {
    6. ...
    7.     RaycastHit hit;
    8.     if (Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit))
    9.     {
    10.         target = hit.point;
    11.         Debug.Log(target);
    12.     }
    13. ...
    14. }
    Not only is there nothing being detected if I go past the ball, but no matter where over the ground I hover over, the Z value of my target (as told by Debug.Log(target)) is always -10.10. The ground is a cube object at world space (0, 0, 30) with a scale of (50, 0.1, 100).
    upload_2023-6-3_1-39-51.png
     
    Last edited: Jun 3, 2023
  2. FedoraKirb

    FedoraKirb

    Joined:
    Apr 20, 2021
    Posts:
    6
    The problem seems to be that the ray being drawn is not travelling far enough, as the coordinates being hit match with the ray at a specific distance. But I only pass the ray and RaycastHit arguments, so the ray should be infintie.
     
  3. FedoraKirb

    FedoraKirb

    Joined:
    Apr 20, 2021
    Posts:
    6
    Sorry, I figured out what was happening, it was hitting an invisible wall in the scene before it hit the ground, cutting off the Ray. Resolved.