Search Unity

Accessing parts of a model from a script

Discussion in 'Scripting' started by falcon72, Nov 30, 2010.

  1. falcon72

    falcon72

    Joined:
    Nov 30, 2010
    Posts:
    2
    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:
    Code (csharp):
    1. // Script to shoot projectile
    2.  
    3. var Fighter : GameObject;               // The main fighter
    4. var ProjectilePrefab : Rigidbody;       // The bullet prefab to be used
    5. var ProjectileSpeed : int = 40;      // Speed of the bullets
    6. private var resultantspeed : int = 0;
    7.  
    8.  
    9. function Update () {
    10.     if (Input.GetButtonDown("Fire1")) {
    11.         // Fire1 button detected
    12.        
    13.         // Get the script component
    14.         var flightcontroller : Fighter_Character = Fighter.GetComponent(Fighter_Character);
    15.        
    16.         resultantspeed = flightcontroller.flightspeed;
    17.         // Get the speed of the projectile and add it to the speed of the fighter
    18.         resultantspeed = resultantspeed + ProjectileSpeed;
    19.        
    20.         // Create new projectile
    21.         var Projectile : Rigidbody = Instantiate(ProjectilePrefab, transform.position, transform.rotation);
    22.        
    23.         // Set the projectiles velocity in the z direction but transfrom from local to world orientation
    24.         Projectile.velocity = transform.TransformDirection(Vector3(0,0,resultantspeed));
    25.        
    26.         // Make sure that the bullet does not collide with the gun muzzle object
    27.         Physics.IgnoreCollision(Projectile.collider, transform.root.collider);
    28.     }
    29.        
    30. }
     
  2. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Move your firing code out of the Update and into its own custom method, then just call that method whenever you want to fire:

    on your spaceship:
    Code (csharp):
    1. function Fire()
    2. {
    3.     this.GetComponent("Gun_Left").Fire();
    4.     this.GetComponent("Gun_Right").Fire();
    5. }
    On your guns:
    Code (csharp):
    1.  
    2. function Update()
    3. {
    4.     if (Input.GetButtonDown("Fire1"))
    5.     {
    6.         // Fire1 button detected
    7.         this.Fire();
    8.     }
    9. }
    10.  
    11. function Fire()
    12. {
    13.     // Get the script component
    14.     var flightcontroller : Fighter_Character = Fighter.GetComponent(Fighter_Character);
    15.    
    16.     resultantspeed = flightcontroller.flightspeed;
    17.  
    18.     // Get the speed of the projectile and add it to the speed of the fighter
    19.     resultantspeed = resultantspeed + ProjectileSpeed;
    20.    
    21.     // Create new projectile
    22.     var Projectile : Rigidbody = Instantiate(ProjectilePrefab, transform.position, transform.rotation);
    23.    
    24.     // Set the projectiles velocity in the z direction but transfrom from local to world orientation
    25.     Projectile.velocity = transform.TransformDirection(Vector3(0,0,resultantspeed));
    26.    
    27.     // Make sure that the bullet does not collide with the gun muzzle object
    28.     Physics.IgnoreCollision(Projectile.collider, transform.root.collider);
    29. }
    Just whipped this together, adapt it as you need to.
     
  3. falcon72

    falcon72

    Joined:
    Nov 30, 2010
    Posts:
    2
    Hi,

    Thanks for the info, but this is not what I'm looking for. I would like to completely take the firing script out of my guns and only have it in the script attached to my script.
     
  4. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Ok, do it that way then. What's the problem?