x


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

more ▼

asked Nov 23 '10 at 01:32 PM

crums gravatar image

crums
13 7 9 12

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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

answered Nov 23 '10 at 03:25 PM

Tom 10 gravatar image

Tom 10
287 2 3 7

this just seems to make the ball bounce of the spheres. am i doing something simple but wrong?

Nov 23 '10 at 05:00 PM crums

make sure your spheres are marked as triggers

Nov 23 '10 at 07:12 PM Tetrad
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x384
x184
x63

asked: Nov 23 '10 at 01:32 PM

Seen: 545 times

Last Updated: Nov 23 '10 at 01:32 PM