Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dave M · Dec 16, 2010 at 01:45 AM · triggerscorepoint

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by The_r0nin · Dec 16, 2010 at 07:36 PM

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

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dave M · Dec 16, 2010 at 10:14 PM 0
Share

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

avatar image The_r0nin · Dec 16, 2010 at 11:09 PM 0
Share

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

avatar image Dave M · Dec 16, 2010 at 11:47 PM 0
Share

special Thanks.It works fine...well done!!!

avatar image The_r0nin · Dec 17, 2010 at 12:04 AM 0
Share

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.

avatar image Dave M · Dec 17, 2010 at 12:22 AM 0
Share

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??

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

After I initially add score it keeps adding. 2 Answers

OnTriggerEnter to load new level 1 Answer

Point system help! 2 Answers

Point system, something wrong 1 Answer

Score only adds 1 then stops counting!! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges