- Home /
Gun shooting sideways. NEED HELP!!!
Okay Im starting on making a FPS game and I have a code for shooting animation, and when I test it I shoot forward but when i look around I sometimes shoot on a weird angle and if I look back its fine again. NEED HELP!!
Here is my code
var bullet : Rigidbody; //Your bullet prefab. Should have a RigidBody
var speed : float = 100.0f;
var muzzlePoint : Transform; //Your SpawnPoint
function Update() {
if(Input.GetButtonDown("Fire1")) {
var instance : Rigidbody = Instantiate(bullet, muzzlePoint.position,
muzzlePoint.rotation);
instance.velocity = muzzlePoint.forward * speed;
}
}
I think it's got to be the muzzlepoint transform. Everything else is fairly obviously straightforward.
Do a debug.log printout of the muzzlePoint position and rotation, as well as the bullet position and rotation as the last command in your if() block. That should be very revealing.
What is the hierarchy of your objects? If you want to always shoot in the direction you are looking, your camera and muzzle point transforms need to match. Are they both connected to the same parent - or is one a child of the other?
Answer by ScottW · Oct 24, 2012 at 07:51 PM
Where does muzzlePoint get set? In the inspector?
Try transform.rotation and transform.forward instead of muzzlePoint.rotation and muzzlePoint.forward. This will give the instantiated object the same heading as it's transform.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082813im_/https://answers.unity.com/themes/thub/images/avi.jpg)