Keeping gun on straight axis

Does unity have object constraints?
I’m working on a top down shooter, with bullets spawning from a spawn point at the gun muzzle, and some animations are skewing the bullets all over the place…
Is there a way to lock the gun to an axis, to ensure straight bullets (preventing bullets from sloping up or down)

I’m avoiding using a rigidbody

2 Answers

2

if your gun has a rigidbody attached to it, you can prevent it from rotating by checking the boxes in Constrains ----> freeze position.
if your gun doesn’t has a rigidbody, use

transform.position.x = 0; //or whatever axis/position you want

I'm avoiding rigidbody because I don't want it to have it's own gravity, and I don't know how to approach adjusting the transform position/rotation of the gun as this is changing every second..

–

Add a rigidbody and uncheck “Gravity”. If you do not want gravity then do that. Maybe set the object to always be at Vector3.Forward so it is always facing that direction. Please upload your script so I can tell you what you need to do. It helps to see what’s “Under The Hood” sometimes :wink:

I was thinking of forward as well, was going to set it as the player's forward, but would this have to be updated every frame in Update or does Unity provide a static for this?

–