- Home /
loss of life help
i am trying to create a sphere that when my character touches it it dies/respawns from the starting place, level isnt restarted. im trying to make it so that these spheres can only be touched 3 times throughout the whole game and on the fourth touch its game over. please could someone help me with my coding? as im getting no where
Answer by Tom 10 · Nov 23, 2010 at 03:25 PM
There are several ways this could be accomplished, depending on the rest of your game. Here's one simple way....
Here's a script to attach to your player GameObject:
public static var instance:Player = null;
public var startingPlace:Transform; // Drag the spawn point GameObject here in the editor public var respawnLimit:int = 3; // How many times the player can hit a sphere (this allows you to configure it in the editor)
private var respawnCount:int; // Used for counting how many times the player hits a sphere and respawns
function Awake():void { instance = this; respawnCount = 0; }
function Respawn():void { if(++respawnCount > respawnLimit) { // Game over! } else { transform.position = startingPlace.position; transform.rotation = startingPlace.rotation; } }
Here's a script to attach to your sphere GameObjects:
function OnTriggerEnter(other:Collider):void {
Player.instance.Respawn(); // NOTE: This assumes you named the above script Player.js. If you name it something else, replace "Player" on this line with the name of the script.
}
this just seems to make the ball bounce of the spheres. am i doing something simple but wrong?
Your answer
Follow this Question
Related Questions
Water is killing player when enemy enters it 1 Answer
kill boundary script 1 Answer
Detect different death sources? 2 Answers
Player Respawn After Death 1 Answer