- Home /
I made a script that shoots but the bullet's rotation is wrong when instantiated?
I made a script that shoots but the bullet's rotation is wrong when instantiated?I'm not sure how to rotate it, here's my script:
using UnityEngine;
using System.Collections;
public class Theshoot : MonoBehaviour { public Rigidbody bulletPrefab; public Transform barrelEnd;
void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
Rigidbody rocketInstance;
rocketInstance = Instantiate(bulletPrefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
rocketInstance.AddForce(barrelEnd.forward * 5000);
}
}
}
$$anonymous$$ost Unity code assumes that if your object has a rotation of (0,0,0), the front of the object is facing positive 'z'. For your code to work above, both your barrelEnd and your bullet must have this rotation.
Answer by joedartjr · Jan 20, 2014 at 01:45 AM
Make a new Empty Game Object and set it to position (0,0,0) with no rotation. Then rotate the Game Object you are using as a bullet to the way you want it to come out when you "fire" it. Then drop it into the Empty Game Object, and save that as a prefab (named whatever you want). Use that as your bulletPrefab. Then it should instantiate the Empty Object with no-rotation, but your bullet will be whatever direction you rotated it. If you need to change the bullets rotation, simply rotate the bullet Game Object inside of the Empty Game Object. The Empty Game Object's rotation should always be (0,0,0).
I would use this method but also, using (Quaternion.Identity) is the best way to make sure spawned objects have the same rotation as the object spawning them.
Answer by tw1st3d · Oct 25, 2013 at 06:33 PM
Try doing
rocketInstance = Instantiate(bulletPrefab, barrelEnd.position, bulletPrefab.transform.rotation) as Rigidbody;
So you're calling the bullets rotation, not the barrels.