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
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.
}