Search Unity

Physics.Raycast() hit is (0,0,0) always time

Discussion in 'Getting Started' started by vuqar97, Jun 3, 2019.

  1. vuqar97

    vuqar97

    Joined:
    Jan 28, 2019
    Posts:
    1
    Code (CSharp):
    1. private int pointIndex;
    2.         private PathCreator pathCreator;
    3.  
    4.         private void OnMouseDrag()
    5.         {
    6.             if (pathCreator != null)
    7.             {
    8.                 var rayMoving = Camera.main.ScreenPointToRay(Input.mousePosition);
    9.                 Physics.Raycast(rayMoving, out var hitMoving);
    10.                 transform.position = hitMoving.transform.position;
    11.                 pathCreator.bezierPath.MovePoint(pointIndex, hitMoving.transform.position);
    12.                 pathCreator.gameObject.GetComponent<RoadMeshCreator>().CreatePath();
    13.                 Debug.Log("Position to: " + hitMoving.transform.position);
    14.             }
    15.         }
    Why should be this?
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,445
    The return value of Raycast is bool. If it returns
    false
    , no hit was detected and everything in the
    out
    parameter is reset to default values (not left unwritten which some might expect). Usually people say
    if (Raycast...)
    and only proceed if the ray hit. I expect you're never detecting a hit due to layer or collider issues.
     
    JoeStrout likes this.