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…