Easy scripting issue??

Hi All,

I have an issue with my c# script and I can’t find an answer.
I have a space ship which fires rockets.
The rockets are fired via the following script:

using UnityEngine;
using System.Collections;

public class RocketAI : MonoBehaviour {
	
		public float speed;

	// Use this for initialization
	void Start () {
		Vector3 newVelocity = Vector3.zero;
		newVelocity.y = speed;
		rigidbody.velocity = newVelocity;
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

My issue is the rockets only fire upward. I need them to fire in whatever direction the space ship is facing. Can anyone help me sort this out please?

Thank you all kindly.

Use rigidbody.AddForce, with transform.forward being involved in the force parameter. You could also use that along with setting the velocity directly, but AddForce gives you more options.

–Eric

Thanks Eric. I’ve been looking into this. However the rockets are still going upward. Not sure what I’m doing wrong here. How would you implement what you said as a piece of written code?

Thanks Aliput

If you want the rocket to travel in a straight line from where you instantiated it, something like this should work.

Vector3 newVelocity = transform.forward * speed;
rigidbody.velocity = newVelocity;

You’re not using transform.forward.

–Eric