- Home /
Raycast hitting below mouse position :(
Hello Unity people, i'm making a third person shooter and I'm facing some difficulties figuring out the best way for aiming the gun.
What i did was creating a camera in the hole of the gun that casts a ray to the mouse position, i used Debug.DrawLine to see where the ray is going, and it always goes below the mouse.
This is my script: #pragma strict var rayOrigin : Camera; function Start () {
}
function Update () {
var ray = rayOrigin.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, Mathf.Infinity)) {
Debug.DrawLine (ray.origin, hit.point, Color.red);
Debug.LogError("you hit something");
}
}
i was pointing at the center of the cube in my game view, but the ray was pointing below it, like in this picture
Help me please
Is what you want here for the ray to go from the gun to the position under the mouse cursor? What do you want to happen if the cursor is behind the person? Below the person? What happens if the cursor is not over an object?
After reading your comment, i realized that my way of making the gun shoot is wrong, i'm going to look for other examples to figure out a better way, thank you
It's not wrong, but you have to consider all the situations and what you want to happen. And I needed to understand what you wanted to happen to give you an answer.