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 jeff · Jun 17, 2012 at 03:13 PM · triggerracingentercheckpoint

Enter / Exit Trigger

Hey guys,

I am currently writing a script for a racing game that keeps track of how many checkpoints a racer had passed. The checkpoints are triggers and when a racer enters (OnTriggerEnter)a trigger they get 1 added to their checkPointsReached.If a racer decides to go backwards through a trigger a checkpoint is decremented when they exit(OnTriggerExit) the trigger.

The problem I am having is when a racer slightly enters a trigger then exits out the same way. For example if a racer is heading toward checkpoint 3 - slightly enters incrementing the checkpoints to 4 but then leaves the same way, the script will be searching for checkpoint 4 when in fact the racer would be heading toward checkpoint 2. The same happens if a racer passes a checkpoint , say checkpoint 5 then turns around and slightly re-enters checkpoint 5 but then turns back the correct way , the OnTriggerExit will activate and decrement the number reached even though the racer is going the correct way.

I could double up every checkpoint , give them a little space and have each one respond to a separate trigger, however, on some stages there are quite a large amount so would become very tedious.

I have also experimented with having the script only check every second checkpoint for going the wrong way but it causes problems at the start and end and results in very convoluted code.

Any help is appreciated!

Thank you

Jeff

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

2 Replies

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

Answer by jeff · Jun 21, 2012 at 02:27 PM

I ended up adding an extra checkpoint behind every checkpoint .The first checkpoint incremented the checkpoints reached and the second decremented. Bools were used to check if it was appropriate to increment/decrement i.e if a racer had gone through the first check point a front bool is set to true then if they carry on and go through the second checkpoint it would check to see if front was true before incrementing/decrementing. I used OnTriggerEnter for incrementing and OnTriggerExit for decrementing to stop any confusion.

Comment
Add comment · Show 1 · 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 whydoidoit · Jun 21, 2012 at 02:29 PM 0
Share

Nice work! $$anonymous$$ark it as answered to get it off this list :)

avatar image
0

Answer by pheash · Jun 17, 2012 at 07:46 PM

Hi Jeff, I am a bit confused as to what you think OnTriggerExit and OnTriggerEnter do. are those checkpoints static in the world? if they are just a box trigger on the track as soon as the car drives over the trigger, OnTriggerEnter is called, when it leaves the box OnTriggerExit is called. that might confuse your script. I would suggest that you have a little script attached to your checkpoint that contains a boolean iWasPassed:false. you could then set that one to true in your OnTriggerEnter function and have an if statement to check whether it is already checked or not. I write some code here but i cant test it. this is for your checkpoint:

 var iWasPassed : booelan = false;
 function OnTriggerEnter(other : Collider){
   iWasPassed=!iWasPassed //this sets the boolean to the opposite state. first time to true.
    if(iWasPassed){
     other.transform.GetComponent(CarScript).checkPointsReached++;
   }
   else{
      other.transform.GetComponent(CarScript).checkPointsReached--;
   }
 
 }

This assumes that the script that contains the variable checkPointsReached is attached to the car and called CarScript..

I hope that helps somewhat

Comment
Add comment · Show 5 · 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 jeff · Jun 17, 2012 at 11:09 PM 0
Share

Hey Pheash,

Thanks for your answer. I feel this may have the same problem. If the car enters the trigger for the first time iWasPassed will be false and checkPointsReached will be incremented. That works well ,however, what if the car turns around and re-enters the checkpoint but does not fully go through? iWasPassed will be true and checkPointsReached will be decremented but because the car didnt fully go through the checkpoint they wouldnt re-trigger it and would carry on with checkPointsReached being one less than it should.

It is a very rare case when a player is driving properly but on occasion if a wall is hit and something strange happens the problem arises.

avatar image whydoidoit · Jun 17, 2012 at 11:11 PM 0
Share

How about checking an already_entered boolean when the trigger is entered and don't do the code if it is already set. You could then have another trigger somewhere to reset the checkpoints boolean.

avatar image jeff · Jun 17, 2012 at 11:19 PM 0
Share

Hey whydoitdoit,

Do you mean to have one trigger to reset all checkpoints or one for each checkpoint? Also what if they decide to go backwards before that trigger resets ?

Thanks for help

avatar image pheash · Jun 18, 2012 at 07:21 AM 0
Share

hey Jeff, I see what you mean, maybe you want to make the checkpoint colliders smaller, for example the bit on the car that collides with the checkpoint could be rather small, it doesnt have to be the cars boxcollider. just make an extra object that is parented to the car but is only about the size of a wheel. I know the above problem could still occur but that way it would be very improbable. the only other way that comes to my $$anonymous$$d would be to check whether the car is to the left or right of the checkpoint once it comes into a certain range. this would mean you need to check the distance between car and checkpoint and if its small enough check the direction the of the car compared to the direction of the checkpoint. that way you would have to orient your checkpoints to all point along the track.

avatar image jeff · Jun 21, 2012 at 01:14 PM 0
Share

Thanks for your help pheash and whydoitdoit!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GUI script problem with trigger enter. 1 Answer

How do I have the object disappear once the FPS controller looks at and then looks away. 0 Answers

Collision between objects? 2 Answers

About "How do I make laps in a racing game?" 2 Answers

Trigger and Collision Events do not work. 0 Answers


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