x


How can I make triggering multiple triggers give more points?

I have a score system like this:

Everytime my ball enters my trigger, I get 100 points. But what if I want to get 300 points if my ball enters two triggers, or if my ball enters three triggers I get 500 points?

This is my current code, which doesn't account for multiple triggers:

static var myScore : int = 0;

function OnTriggerEnter()
{
    myScore += 100;
    Debug.Log(myScore);
}

function Update () 
{
    if (myScore >= 1000) 
    {
        Application.LoadLevel (1);
    }
}

I hope you understand this, and maybe somebody could please help me?

more ▼

asked Dec 16 '10 at 01:45 AM

Dave M gravatar image

Dave M
20 3 3 7

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

1 answer: sort voted first

Assuming that your triggers are stationary and do not get destroyed when hit: Add a class variable (call it "timeOfHit" or something) for the time and a class variable for the last trigger hit (you'll need to tag your triggers for a function... more on this later). Then when you hit a trigger record the Time (timeOfHit = Time.time;) and the tag (you'll need to have the trigger return a number or string when the function is called). On the next collision, check to see if the time is close enough to the last time (whatever you want your threshold to be) and you are hitting a different trigger. If so, give multiple points...

On the player:

var timeOfHit:float =0.0;
var lastTriggerHit: int = 0;
var playerScore: int = 0;

function HitScore(triggerNumber:int){
    var thisHitTime:float = Time.time;

    if (triggerNumber != lastTriggerHit){
        lastTriggerHit = triggerNumber;
        if ((thisHitTime-timeOfHit) > 2.0){
            playerScore += 100;
        }
        else{
            playerScore +=300;
        }
        timeOfHit = thisHitTime;
    }
}

On the Trigger:

var myNumber:int = 1; // different value for each trigger

OnTriggerEnter(other:Collider){
    other.gameObject.BroadcastMessage("HitScore",myNumber);
}

This should work (I haven't tested it)... the important thing is to record which trigger was hit and how long ago (if you want to make the player hit nearby triggers close together for an increased score).

more ▼

answered Dec 16 '10 at 07:36 PM

The_r0nin gravatar image

The_r0nin
1.4k 9 14 30

Thank you for your help but something dont work properly.I get the message score player.js(16,21): BCE0005: Unknown identifier: 'thisTime'.

Dec 16 '10 at 10:14 PM Dave M

The variable should be thisHitTime in both places. I've edited the script above.

Dec 16 '10 at 11:09 PM The_r0nin

special http://Thanks.It works fine...well done!!!

Dec 16 '10 at 11:47 PM Dave M

Don't forget to click the checkmark to the left of the answer (so everyone will know the problem has been solved). Glad I could help.

Dec 17 '10 at 12:04 AM The_r0nin

ok one last question maybe you could help me again....the score of the player is shown but my player is a ball (prefab) and the score will be shown correctly untill my prefab is destroyed.then the counter starts from 0. Is there a way to sum the points every prefab got in his lifetime untill he gets destroyed??

Dec 17 '10 at 12:22 AM Dave M
(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:

x986
x274
x132

asked: Dec 16 '10 at 01:45 AM

Seen: 797 times

Last Updated: Dec 17 '10 at 02:09 AM