Hi. I have a model of a gun,which is by the way paranted to my caracter´s camera(it´s a first person character). I was trying to make a instantiation of bullets from the gun,but the rotation of the bullets path gets weird when I look too much for up or down,I mean,they don´t follow the rotation of the gun. I also tried to create a empty object and place it on the tip of the gun so that it would make the paper of instantiating the bullets,but the same problem involving their rotation happened. Here are some images that may help me explain the situation(oh,and sorry for my english)
how do you instantiate the bullets there? (can you show code?)
I can’t show the entire code right now,but it’s basically this(this is assigned to the weapon’s object)
var Bullet :GameObject;
If(Input.GetMouseButton(0))
{
Instantiate(Bullet,transform.position,transform.rotation)
}
after creating the bullet, try bullet.transform.forward = gun.transform.forward
don’t parent the bullet to the gun after its fired… move it seperately… jsut use gun muzzle position to get position and orientation.
bullet
move it like this…
Vector3 moveVector = gun.transform.forward.normalized;
bullet.transform.position = bullet.transform.position + moveVEctor * bulletSpeed * Time.deltaTime
This should work as long as your bullet and gun point forward along the Z / forward axis in their default positions… if the bullet shoots 90 degrees to the side or up… then you’ll need to use transform.right or transform.up instead (or a - in front)
fine,but… How would I acess the bullet transform data and the gun transform data to put it in the same script?
in editor window… browse your hierarchy to the gun… see if there is a nice transform on there that corresponds to the muzzle position, or a bullet object. Personally, I would add my own node to the the gun as a child at the position of the muzzle… Then you can use script to spawn a bullet prefab at that muzzle transform position whenever you like… and muzzle flash or smoke effects.
Add a script to the gun called… gun.cs might work. Expose a variable… public Transform muzzleTransform. then drag your new muzzle node object onto that variable. Now its pretty easy in script to reference that muzzle transforms’ orientation or position. Or to spawn new bullet or object at its position…