Hello,
I’ve tried searching for this using google and via the community but can’t seem to find any reference to it.
My current situation is this:
I have a model of a spaceship (created using Maya) which also includes 2 place holders called Gun_Left and Gun_Right from which I emit my bullets whenever the “fire” button is pressed. The way the bullets are emitted is via a script which is attached to both Gun_Left and Gun_Right. i.e. 1 script attached to 2 points on the model.
My question:
Is there a way in which I can have a single script which is attached to my root spaceship model (Fighter_2_4) so that I can use just that script to fire using the position and rotation of my gun ports?
My model hierarchy is as follows:
Fighter_2_4
|group2
|group3
| Gun_Left
| Gun_Right
My current script looks like this:
// Script to shoot projectile
var Fighter : GameObject; // The main fighter
var ProjectilePrefab : Rigidbody; // The bullet prefab to be used
var ProjectileSpeed : int = 40; // Speed of the bullets
private var resultantspeed : int = 0;
function Update () {
if (Input.GetButtonDown("Fire1")) {
// Fire1 button detected
// Get the script component
var flightcontroller : Fighter_Character = Fighter.GetComponent(Fighter_Character);
resultantspeed = flightcontroller.flightspeed;
// Get the speed of the projectile and add it to the speed of the fighter
resultantspeed = resultantspeed + ProjectileSpeed;
// Create new projectile
var Projectile : Rigidbody = Instantiate(ProjectilePrefab, transform.position, transform.rotation);
// Set the projectiles velocity in the z direction but transfrom from local to world orientation
Projectile.velocity = transform.TransformDirection(Vector3(0,0,resultantspeed));
// Make sure that the bullet does not collide with the gun muzzle object
Physics.IgnoreCollision(Projectile.collider, transform.root.collider);
}
}