Home > database >  Unity Raycasthit.Point Acting Weird
Unity Raycasthit.Point Acting Weird

Time:07-24

I'm trying to transport positions of a gameObject according to mouse position in 3D. However, When I do that with Raycasthit.point object is coming to player camera position like in this video

Is there anyone who can help me please

-> My Code:

 void Update()
    {
        if (currentObj != null)
        {
            currentObj.transform.position = MouseToWorldPositions();
        }
    }

    public Vector3 MouseToWorldPositions()
    {
        Ray ray = playerCam.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out RaycastHit hit, 999f, floorLayer))
        {
            return hit.point;
        }
        else
        {
            return Vector3.zero;
        }
    }

CodePudding user response:

Are you sure your building blocks are not on the floorLayer mask? It seems that the raycast is hitting the edge of the building schematic and returning that point.

  • Related