- Home /
shoot ray from mouse cursor
I am shooting a ray from the mouse cursor to see what object I hit. I have a gameObject that follows the mouse cursor like a gunsight.
I am shooting a gun .
The problem is that I am detecting collisions with objects that I should miss , when the mouse cursor moves above or below and object.
transform.position = Camera.main.ScreenToWorldPoint(mouseP); fwd = transform.TransformDirection(Vector3.forward); Debug.DrawRay(transform.position, fwd * 100, Color.green);
if(Physics.Raycast(transform.position, fwd,out hit)) { Debug.Log("Hit=" + hit.collider.gameObject.name);
Answer by syclamoth · Jun 10, 2012 at 01:21 PM
In this situation, the problem arises because you are overcomplicating things. You have a bit of code which as far as I can tell is intended to move the 'crosshair' to the position of the mouse:
transform.position = Camera.main.ScreenToWorldPoint(mouseP);
However, the mouse is a 2-dimesional object on the surface of the 'window' into your 3-dimensional world. As such, it shouldn't really ever move!
If you want to shoot raycasts at the mouse's position, it is as simple as using
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
This will create a ray with the correct position and direction to be used as a mouse-click detector.
As for drawing a crosshair, you can then use
transform.position = ray.GetPoint(however far away from the camera);
to determine the position of the crosshair object.
Ok so the ray shoots from the gunsight located just in front of the fps to the mouse cursor direction?
I need the ray to shoot through the mouse cursor because the mouse cursor in 3d world is just where the camera is on the fps which is behind the player.\
I dont think it is as simple as your saying.
It really is just this simple. The raycast $$anonymous$$UST come from the exact location of the camera, or it will not be valid. ScreenPointToRay does exactly what you need it to- unless your definition of mouse cursor is something strange. Are you using a 3d mouse or something?
This Ray come from the camera and is drawn to the mouse cursor. The mouse cursor is only the camera position so I am confused. If I convert the mouse cursor into world values then I just get the camera position?
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
A 'ray' isn't a 'position'- it's a 'position' and a 'direction'. Hence, 'ScreenPointToRay' gives you a ray whose 'position' is the same as that of the camera's, but whose 'direction' is pointing where the cursor is!
In fact, a ray in this situation is the only sensible way to model the position of a 2-dimensional mouse cursor in a 3-dimensional space. You see, the mouse could be in any one of an infinite number of positions, because you don't know how far away from the camera you want it to be! A ray takes this into account, because it represents all the possible points that the mouse could be over top of, in 3d space.
The mouse cursor is located at a 3D point transform.position = Camera.main.ScreenToWorldPoint(mouseP); but as you say the direction is the most important and this is what it does.
The below code tests for hits for a distance of 100 (100m).
I can also test for just an infinite direction
if(Physics.Raycast(ray,out hit)) {
Your answer
Follow this Question
Related Questions
Shooting automatic guns. 1 Answer
On Lerpz game making shooting monster 1 Answer
Make AI enemies react to bullet hits (Solved) 0 Answers
2D Bullet texture follow ray and Bullet Holes 1 Answer
How to make my enemy bleed 1 Answer