Space Shooter: Instantiate rocket and fire it later

Hi guys,

I am going crazy and need your help.
I am trying to add some stuff to the space shooter tutorial and somehow cannot make this work.

My problem is that I would like to add an additional weapon (a big rocket) that is visible all the time (in contrast to the regular laser bolt). When it is shot, it should be reinstantiated after a certain time.

Right now I have one script on the rocket object that is listening for player input (space). I have declared a public variable on the player object (public bool wasShot) which should be set to true when the rocket is fired.

The rocket should be reinstantiated after one second (i.e. become visible again after one second).
Thus the code would be something like:

    void Update()
    {   

        if (wasShot = true && Time.time > nextFire){
            Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
            wasShot = false;
        }

How can I access and change this boolean variable on the player object from the rocket object?

Any help is highly appreciated!

I suggest a different design. Create a player class and a rocket class! The rocket class has a public method called “shoot” that makes the rocket itself invisible and “Instantiates” a rocket prefab that moves to its target. After one second the rocket class makes itself visible again and the “shoot” method can be called again by the player object.

That way the player doesn’t care for the “wasShot” variable!

1 Like

Hm sounds like a good idea, but how do I keep the rocket moving with the ship then? Do I make it a child of the Player object? Also I would like to spawn it either on the left or the right wing, depending on the current movement on the x-axis.

ya instantiante it as a child of the player, and wehn you instance it, grab a reference of it on your player script, so you can us eits fire() method.

For multiple rockets you could use List to hold references to multiple rockets which you can remove and add from thje list with .add() and .pop()

But what is so much better about this than simply changing the bool value like I suggested in my first post? Is it possible to access boolean values from other scripts?

Find the player object from your rocket script, somehow.
If your rocket is parented to the player, it would be as simple as this:

class Rocket : MonoBehaviour {
    Player player;

    public Start() {
         if (transform.parent)
               player = transform.parent.GetComponent<Player>();


        if (!player)
              throw UnityException("Rocket was either not parented to anything, or the object it was parented to does not have a Player monobehaviour.");
    }


}

@Masterchiefs : Yes, it is possible to access boolean attributes from other scripts but it’s bad practice! The principle behind this is called: Encapsulation

Ok I am trying to do the encapsulation thing :slight_smile:
I have made the shot object a child of the player object, however now the player input is not really applied to the shot object. The player is moving but the shot only moves a bit left and right and is not really following the player object any more.
When I move the Player ship in the inspector, the rocket is moving fine but in play mode it does not.
Ideas?

Here is my player movement code; I think this should be applied automatically to all the child objects as well!?

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (moveHorizontal, moveVertical, 0.0f);
        rigidbody.velocity = movement * speed;
}

Ok apparently you should never have two rigidbodies as parent/child.
But how else can you do it? I mean the ship has to have a rigidbody to detect collisions and the rockets also have to have a rigidbody to be able to shoot anything.
Joints?

I don’t really understand your question, now. If you made the rocket a child of your player, you don’t need to update the position of your rocket. The rocket will stick to the player as long as it is a child.

No it seems like they don’t stick together because both are rigidbodies and they exert forces on each other.

just disable the rigid body till you fire it.

also if it is just a rocket that is not affected by gravity, and cant be pushed by other things, dont give it a rigid body, just move it using its transform.

either way you can have it move with the ship by it being a child of hte transform