Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help to stop a bullet object firing when renderer is disabled???

Discussion in 'Scripting' started by dillo204, May 5, 2014.

  1. dillo204

    dillo204

    Joined:
    Apr 3, 2014
    Posts:
    10
    Hi,

    I have made a simple shooter "type" game where when the players ship gets hit by an enemy ship, it explodes, loses one life and disappears for 2 seconds then comes back. The problem I'm having is stopping the ships bullets firing when the renderer's are disabled for that 2 seconds!! Can anyone help please????
    Code (csharp):
    1.        
    2.  
    3.         if(Input.GetMouseButtonDown(0)){
    4.     audio.PlayOneShot(pew);
    5.     Instantiate(bullet, transform.position + new Vector3(0,3,0),
    6.     Quaternion.identity);
    7.     }
    8.     }
    9.    
    10.    
    11.     function OnCollisionEnter(col : Collision) {
    12.  
    13.     if (col.gameObject.tag == "bomb");
    14.     renderer.enabled = false;
    15.     collider.enabled = false;
    16.     Respawn();
    17.         }
    18.  
    I have also got a bullet prefab and a script for the bullet which is this:
    Code (csharp):
    1.  
    2.    function Start () {
    3.     rigidbody.velocity.y = 100;
    4.         }
    5.  
    6.    function Update () {
    7.     if(transform.position.y > 62){
    8.         Destroy(gameObject);
    9.     }
    10. }
    11.  
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    If you the enemy is always in the frustum, you could use

    Code (csharp):
    1.  
    2. if(renderer.isVisible)
    3. {
    4. // your code
    5. }
    6.  
    since this would stop them as soon as your renderer is disabled.

    However, if the enemy should also shoot if he cannot be seen by the camera, i.e. hes behind you, you should rather make a simple boolean variable which will be set to false whenever your renderer is turned off, and back to true when it get enabled again.

    Check on the enemy ship's script whether the variable is true and let it shoot when it's set, if not, then not.