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?
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.
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?