- Home /
Problem with bullet not rotating with gun
My bullet dose not rotate with the gun when i fire (i rotate the gun in play mode and the bullet still comes out like its facing forward)
heres my script
var bulletPrephab : Transform ;
function Update () {
if(Input.GetButtonDown("Z"))
{
var bullet = Instantiate(bulletPrephab, GameObject.Find("Spawn Point") . transform.position, Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward * 2000);
}
}
Answer by Borgo · Jun 28, 2011 at 07:51 PM
Maybe works if you apply the same object rotation:
var bullet = Instantiate(bulletPrephab, GameObject.Find("Spawn Point") . transform.position, transform.rotation);
Try to change the Quaternion.identity to transform.rotation.
Sorry if I don't understand the question.
helpful for rotating the bullets, but they still don't travel in the correct direction
Answer by GuyTidhar · Jun 28, 2011 at 07:35 PM
You should look at the axis of your bulletPrefab and notice how the bullet is faced compared to its axis. For instance, notice the blue axis - is it along the bullet length pointing the same direction the bullet does? (probably not since you have this question). http://unity3d.com/support/documentation/ScriptReference/Transform-forward.html
You can add an empty game object as a parent to the bullet prefab while rotating the empty game object before doing so, so that its forward vector (blue) will point like the bullet does, then make the bullet prefab the child. Next, you should create a new bullet prefab out of this hierarchy and drag this prefab to your script.
Now, if your bullet is not rotated as you wish do
bullet.transform.forward = transform.forward;
After you call the "Instantiate", and your bullet should be fine.
Your answer
Follow this Question
Related Questions
Predict where object will be 2 Answers
bullet rotation as its affected by gravity 2 Answers
Help with gun accuracy in degrees. 3 Answers
Create sprite in place of cursor that has 'lag' on it? 0 Answers
Bullets. Rotation Probelm 0 Answers