How do I create a bullet whose velocity and direction is affected by the velocity and direction of its source?

The idea that I have in mind is to create a variable that is equal to the direction and velocity of the gameobject (WarBird) that WarBirdFly.js (second script shown) is connected to, then add that variable to the “rigidbody.AddRelativeForce(Vector3.forward*BulletSpeed)” statement in BulletScript.js (below).

Here is the script (BulletScript.js) that goes on my bullet prefab:

var BulletSpeed = 100;

function Update (){

rigidbody.AddRelativeForce(Vector3.forward*BulletSpeed);

Destroy();
}

function Destroy(){
yield WaitForSeconds (5);
Destroy (this.gameObject);
}

And here is the script (WarbirdFly) for the vehicle whose velocity and direction I want to affect the bullet:

var forwardSpeed = 5.0;
var turnSpeed : float = 2;
var boostSpeed = 5.0;
private var boostDirection = 0;
private var boost = 0;
    
function Update () {

	var forward = Input.GetAxis("SubspaceForward")+(boost*boostDirection);
	var turnAmount = Input.GetAxis("SubspaceYaw")*turnSpeed;
	var shipDirectionVelocity = rigidbody.velocity.magnitude;
	
	if(Input.GetButtonDown("SubspaceThrust")) {
		boost = boostSpeed;
		}
	if(Input.GetButtonUp("SubspaceThrust")) {
		boost = 0;
		}
	
	if(Input.GetAxis("SubspaceForward") > 0) {
		boostDirection = boost*1;
		}
	if(Input.GetAxis("SubspaceForward") < 0) {
		boostDirection = boost*-1;
		}
	if(Input.GetAxis("SubspaceForward") == 0) {
		boostDirection = 0;
		}
	    	
	rigidbody.AddRelativeForce((Vector3.right*forwardSpeed*forward));
	transform.Rotate(0,0,turnAmount); 
	rigidbody.angularVelocity = Vector3.zero;
	    	
}

I’ve been trying to call the variable using this on BulletScript.js:

var BulletSpeed = 100;
var sscontrolInstance : warbirdFly;
sscontrolInstance = GameObject.Find("Warbird").GetComponent(warbirdFly);
var targetObj : Transform;

 function Update (){

    rigidbody.AddRelativeForce(Vector3.forward*BulletSpeed + sscontrolInstance.shipDirectionVelocity);

I know my var shipDirectionVelocity is dead wrong - I’ve made it lots of other things.

I basically want to make my rigidbody.AddRelativeForce statement (the one on my ship’s gun) into a variable and I want to add that variable to the rigidbody.AddRelativeForce statement on my bullet, right?

By the way, these are relatively slow moving bullets, so rigidbodies should be fine…

From what I can understand, you want to fire a bullet that initially inherints the rotation and velocity of its parent before firing off forward relative to the shooting objects rotation? For illustration I made the bullets instantiated, you could simply set the bullets rotation/position without making a new one.

All you need is AddRelativeForce Vector3.forward, the rotation from the shooter and a direct copy of the shooting objects velocity as the bullet is getting shot. Put it straight into the bullets velocity and it will follow that initial velocity it was fired at. I added two to the Y axis of the spawned bullet otherwise it collides when spawned with the shooting script (I was using cubes).

Example Shooter Script:

#pragma strict

var Bullet : GameObject;
            
function Update ()
{
	if (Input.GetKeyDown("space"))
    {
		var bullet : GameObject =
		Instantiate(Bullet, transform.position 
		+ (Vector3.up*2), transform.rotation);
		bullet.rigidbody.velocity = rigidbody.velocity;
    }
}

Which will fire the bullet with this script:

var BulletSpeed = 100;
 
function Update ()
{
    rigidbody.AddRelativeForce(Vector3.forward*BulletSpeed);
}