Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Spawn after death.

Discussion in 'Scripting' started by Donkey_Puncherello, Apr 2, 2009.

  1. Donkey_Puncherello

    Donkey_Puncherello

    Joined:
    Nov 19, 2008
    Posts:
    30
    I have a vehicle on a racetrack with some bombs scattered. Players health bar gets subtracted by 5 every time they hit a bomb. Once health is at 0, the vehicle object is destroyed. How do I spawn the vehicle at the same place their health went to 0 at, while being centered on the racetrack?

    Also, the track is in the air and the vehicle can fall off the track. In this case, if the vehicle wheels are not making contact with anything for 4 seconds, the vehicle game object is destroyed. How do I spawn the vehicle where they fell off (also centered)?
     
  2. VoxelBoy

    VoxelBoy

    Joined:
    Nov 7, 2008
    Posts:
    240
    First of all, when you detect that the player health has reached 0, store the transform.position of the vehicle object in a variable. The easiest way to respawn something is to have a Prefab for it. A Prefab is a collection of objects that are already setup the way you want them to be when they get added to the hierarchy. Once the vehicle is destroyed after its health reaching 0, you can use Resource.Load to load the prefab and use Instantiate to place a copy of it at the position that you recorded when the player health reached 0.

    In the case of the vehicle falling off the track, what you could do is store the transform.position of the vehicle object every frame into another variable as long as it's touching the road. When it's not been touching the road for 4 seconds, just destroy the object and re-instantiate it in the stored position just like last time.

    Hope this helps.
     
  3. Donkey_Puncherello

    Donkey_Puncherello

    Joined:
    Nov 19, 2008
    Posts:
    30
    That you very much. This seems like an excellent way to execute the spawn effect.