Daniel Lip
Daniel Lip

Reputation: 11325

How can I use raycasthit according to the mouse cursor position?

public Camera mapCamera;

private Camera[] cameras;
private GameObject mouseOvered;

void Update()
    {
        if (Input.GetKeyDown(KeyCode.M))
        {
            if (mapCamera.enabled == false)
            {
                foreach (Camera cam in cameras)
                {
                    cam.enabled = false;
                }
                mapCamera.enabled = true;
            }
            else
            {
                foreach (Camera cam in cameras)
                {
                    cam.enabled = true;
                }
                mapCamera.enabled = false;
            }
        }

        bool rcHit = false;
        Vector3 mouse = Input.mousePosition;
        Ray castPoint = mapCamera.ScreenPointToRay(mouse);
        RaycastHit hit;
        Debug.DrawRay(mapCamera.transform.position, 
        mapCamera.transform.forward * Mathf.Infinity, Color.magenta);
        if (Physics.Raycast(castPoint, out hit, Mathf.Infinity))
        {
            rcHit = true;
        if (mouseOvered != hit.collider.gameObject)
        {
            mouseOvered = hit.collider.gameObject;
        }
        print(mouseOvered.name);
        //do your thing here to apply/change the material
    }
    if (!rcHit && mouseOvered != null)
    {
        //do your thing to undo the material change
        mouseOvered = null;
    }
   }

The problem is when I'm running the game I see the raycast laser still if I change in the Debug.DrawRay form Mathf.Infinity to 1000 and it's not moving according to the mouse cursor position. I'm moving the mouse cursor around but the raycast laser seems to stay at the same place so I guess It's hitting only specific places/objects and not everything ? Not sure what is going on.

If I'm using Mathf.Infinity I don't see the laser of the raycast at all. And when I'm moving the mouse cursor around the space station only once it's detecting object and print it's name.

What I want to do is when I'm moving the mouse cursor around when it's hitting any object of the space station print it's name.

Only if I change on this line to 1000 for example then I see the magenta color:

Debug.DrawRay(mapCamera.transform.position, mapCamera.transform.forward * 1000, Color.magenta);

If it's Mathf.Infinity I don't see any magenta of the laser.

Mag

Upvotes: 0

Views: 868

Answers (1)

yes
yes

Reputation: 967

There is nothing wrong with the raycasting itself, the problem solely lies with what you draw. You dont draw the Ray, but a "Ray" from the camera position along its forward vector.

Replace

Debug.DrawRay(mapCamera.transform.position, mapCamera.transform.forward * Mathf.Infinity, Color.magenta);

With

Debug.DrawRay(castPoint.origin, castPoint.direction * Matf.Infinity, Color.magenta);

Tough im not sure if multiplying with infinity will work. Something like 1000 units (maybe even use mapCamera.farClipPlane as its unlikely a player wants to pick something outside it anyway) should be (more than) sufficient for most scenarios.

Upvotes: 1

Related Questions