# 3d spaceship scene need help making projectiles fire forward

Hey guys. Just need a bit of help really.

Im in the middle of making an asteroids like game with a 3rd person view i have attached the camera to the ship and have it all moving about and what not, but i have a huge problem when i come to firing my projectiles.

I need my projectiles to fire forward in to space and hit the asteroids and blow them up. I do have a script that makes my gun fire forward but when i rotate the bullets start bending off like the guns rotation isnt keeping up with the ships rotation and also when i start moving they look like they stop firing alltogether even though they are still being instantiated.

Im not the best at scripting so please keep it easy to read if you could thanks very much for looking!!

PS

if you need to see my scripts i can show them but god knows what you would make of them lmao

Don't forget that you have an own speed when your ship is flying. So you have to get the speed of your ship and add it to the speed of your projectile. Say you are flying with 150 m/s and your projectiles are flying with speed of 20, they would just look as they are flying backwards because your ships speed is faster than your projectile. Think about it as your are driving with a car, window down and throw something in driving direction. I would get the head back into the car fast..... :)

Just hand in the speed of your ship to the script from your gun. Every time you change your speed you can just send a message at a function in your gun script with the actual speed. Like this: BroadcastMessage("Fire", youractualspeedofship) And it would fire all guns that have this function in it ;) Example from my ship script:

``````if(Input.GetKey("q"))
{
if(trustspeed < maxspeed)
{
trustspeed += 1;
}
}

``````

Now my launcher:

``````function SpeedChanged (goSpeed : float)
{
goTrust= goSpeed;
}

``````

goTrust is the actual speed of the ship and used in the projectile speed in the Fire function:

``````instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed+goTrust));

``````

They will always fly the defined projectile speed value faster than the ship. And if you fire and turn after that, the rockets are flying in the original direction as they should.

Maybe that helps a bit.

greets Hexergirl

Are your projectiles (Bullets) a particle effect, or is it an actual game object?

I'd just make the bullet a prefab, instantiate the prefab with the same rotation as the ship, then put a force on it to go forward, rather than scripting it to go forward... If that makes sense.

``````var projectile : Rigidbody;

var initialSpeed = 20.0;

function Fire () {

var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);

instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));

Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
}

function Update ()
{
if (Input.GetButtonDown("Fire1")) {
Fire();
}
}

``````

That is the code you posted (incase others want to edit it).

I would suggest you change your projectile from a rigidbody to a GameObject and like Justin said, use a prefab with a rigidbody component.

Additionally, you're setting the velocity manually of a rigidbody, and taking the transformdirection from your shooter transform. It seems like when you instantiate, the transform of the rigidbody (or GameObject in my case) is the same as the shooter, so the "forward" should be same for both. Then, instead of using .velocity being changed, I'd use

``````instantiatedProjectile.rigidbody.AddRelativeForce(transform.forward * initialSpeed);

``````

I hope that helps, or at least brings everyone closer to help with your problem :)

Nwqdqasdfashdqasnvgunb3t

Your bullets are moving forward, you’re just in a rotating reference frame, which is creating centrifugal acceleration in your bullets. If you make them travel faster it will lessen the bending, but as long as you are in the process of rotating you’ll see it.