- Home /
How can I achieve precise projectile shooting towards the crosshair in Unity?
Hi.
I'm trying to make a gun that shoots projectiles.
I've placed the gun in the player's arms on the right of the camera, and now when I shoot a projectile it isn't moving towards the center of the screen (where the player is aiming), but a little bit on the right.
If you don't understand what I mean here's an illustration:
Here's what should happen:
Here's what happens:
It would be nice if whole arms would rotate in a nice way, so that even if I use a raycast-based weapon it'd look natural. I've tried using a raycast from the camera towards the camera's forward vector and then using LookAt() on the player's arms, but the arms move in weird positions (for example behind the camera making them not visible).
Please help me, if you know how.
Answer by Dinosaurs · Feb 08, 2018 at 02:52 PM
Do a raycast straight forward from the center of the camera, and store the point it hits. Then when firing the gun, don't fire straight forward; fire in the direction from the muzzle of the gun to the point that was hit by the raycast. And if the raycast doesn't hit anything, fire toward some distant point along the same viewscreen center ray, maybe 100m out.
Hmmm... Okay, I guess, that would work, but is there a way, I can rotate the arms of the player towards the point, where the raycast hits (maybe with some lerping), so that it would look natural? Because with this technique I'm afraid the angle between the projectile trajectory and the gun would sometimes be too big and it could look weird.