How would I go about rotating a bullet after its instantiated. The rocket launcher shoots but the bullets come out vertical instead of horizontal.
#pragma strict
var rocketPrefab : Rigidbody;
var barrelEnd : Transform;
function Update ()
{
if (Input.GetButtonDown("Fire1"))
{
var rocketInstance : Rigidbody;
rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
rocketInstance.AddForce(barrelEnd.forward * 5000);
DestroyObject(rocketInstance.gameObject, 2);
}
}
2 Answers
2
The fact that the bullets come out vertical has to do with the mesh. Your possible solutions:
- Re-author the bullet in a 3D modeling program.
- Make the visible object a child of an empty game object. Rotate the visible object so that the front is pointing towards positive ‘z’ when the parent object has a rotation of (0,0,0).
- Use the RotateMesh editor script to create a clone of your object with the right rotation.
- It can be done in by applying an extra rotation in code. After the instantiate you could do:
rocketInstance.rotation = Quaterion.Euler(0,0,90) * rocketInstance.rotation;
The (0,0,90) was just an example. You would have to figure out the correct rotation.
Use barrelEnd.transform.forward or barrelEnd.transform.up see which one is best for you
or
parent the rocket to an empty gameobject and then rotate the rocket inside the game object
Note that what you want is the front of your bullet pointing towards positive 'z' when the rotation is (0,0,0).
– robertbuwill probably try to write code for it later on. just to lazy right now.
– Fox-HandlerRotating the mesh is by far the best of the four solutions. There are potential issues with solution #2 and #4.
– robertbu