Hello to everyone,
I have a problem with the Shoot scripts, with every single one of them my pistol shoots to the right hand side and not in a straight line, here you have an image:
How do I fix this?
Does your shot have a collider? Is it colliding with the gun or the player himself at the moment of being fired? One quick way to find out is to hit Play, then hit pause, turn off ALL the colliders around the gun and fire, see if it goes straight.
To fix that, you can use layers to specify layers that don’t interact with each other in the Edit → Project Settings → Physics area.
If it does not go straight, then start printing out values of the direction, the velocity, etc. To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
- is this code even running? which parts are running? how often does it run?
- what are the values of the variables involved? Are they initialized?
Knowing this information will help you reason about the behavior you are seeing.
My shot doesn’t have a collider and when I desactivate all the colliers the weapon doesn’t shoot. How do I use layers in the Pshycs area and in what part of the script should I introduce the Debug.Log()?
Go to Project settings\Physics, and at the bottom, there is a matrix for collision layers. However, first try much smaller shot, and spawn it at the front of the gun. The Scripting API in the doc should help you with your Shooting script. ![]()
In whatever parts you THINK are calculating the correct direction.
For instance, if you’re facing +Z then your forward should be (0, 0, 1) roughly.
If your shot is printing that its velocity is (0.1, 0, 1) that would show you the discrepancy, then you can start tracking down what code is setting that direction and why it is wrong.

