- Home /
Fire a raycast to location of image on canvas
I have the following scene below, I have a crosshair on my screen which is positioned on the canvas, what I would like to know is, is it possible to fire a projectile from a position on my player to the point of the crosshair?
I have been using raycasts originally and have a shooting system in place but was just curious what the best way to achieve this would be if possible?
Answer by Mulldor · Jul 21, 2015 at 10:44 PM
If the crosshair is at the mouse position, use
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RayCast hit;
//Then use a raycast
if(Physics.Raycast(ray, out hit, mathf.infinity))
{
execute code
}
That is the most optimal way to do it. You can of course also just locate at what position the crosshair is in XY (Vector2). Then fire a ray from camera to that one instead.
Hi, the crosshair is not at the mouse position, it is fixed slightly above the center of the screen, it is always slightly above the characters head
Your answer
![](https://koobas.hobune.stream/wayback/20220611232411im_/https://answers.unity.com/themes/thub/images/avi.jpg)