• 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
Question by Andrea0913 · Sep 05, 2018 at 08:53 PM · collisioncollision detectioncollision2d

Last collision

Hi.

I'm doing a spinner and i want to detect the last slot the needle touch, because i need to launch an event after the spinner stops.

     void OnTriggerStay2D(Collider2D col)
     {
         if (!_spinner.isStoped)
             return;
 
         if (col.gameObject.tag == ("1")) SpinPosition = 1;
         if (col.gameObject.tag == ("2")) SpinPosition = 2;
         if (col.gameObject.tag == ("3")) SpinPosition = 3;
         if (col.gameObject.tag == ("4")) SpinPosition = 4;
         if (col.gameObject.tag == ("5")) SpinPosition = 5;
         if (col.gameObject.tag == ("6")) SpinPosition = 6;
         if (col.gameObject.tag == ("7")) SpinPosition = 7;
         
 
     }
 
     void SpinCheck ()
     {
         switch (SpinPosition)
         {
             case 1:
                     Debug.Log("Slot 1");
                 break;
             case 2:
                     Debug.Log("Slot 2");
                 break;
             case 3:
                 Debug.Log("Slot 3");
                 break;
             case 4:
                 Debug.Log("Slot 4");
                 break;
             case 5:
                 Debug.Log("Slot 5");
                 break;
             case 6:
                 Debug.Log("Slot 6");
                 break;
             case 7:
                 Debug.Log("Slot 7");     
                 break;
 
         }
 
     }


So, the thing is that, debug log keeps printing the event like this: alt text

i ned it to be called just once.

Comment

People who like this

0 Show 1
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 Casiell · Sep 05, 2018 at 09:33 PM 0
Share

But when are you calling SpinCheck? You didn't show that

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Zarenityx · Sep 05, 2018 at 09:50 PM

OnTriggerStay is called for every frame that an object is within the trigger, so the log will print continuously.

What you should do is set SpinPosition on OnTriggerEnter, and remove the if (!_spinner.isStoped) return;
part. Now, I'm not quite sure how exactly you are setting _spinner.isStoped, but I'm assuming it's set to true when the velocity is low enough, and isn't set again until the spinner is spun again, where you would set it to true.
Something like:

 if(!isStopped && velocity<threshold){
      velocity = 0;
      isStopped = true;
 }

I imagine?
If there's a difference in how this works, the important bit to note is that this only gets called once. When isStopped is true, it doesn't enter the if statement again. Well if it resembles this form at all, the change is simply to:

 if(!isStopped && velocity<threshold){
       velocity = 0;
       isStopped = true;
       SpinCheck(); //Check what it's on only when it stops
  }

Thus:
SpinCheck() is only called once per stop
SpinPosition is only set when it changes
You can fire your event without problems.


On a slightly unrelated note, consider the fact that frankly the physics engine is a bit overkill for something like a spinner, and juggling tags and colliders can end up getting messy over time, as well as feel rather floaty and dull without some serious extra work put into it. You also lose the ability to set probabilities explicitly, which can make balancing the outcomes of those spinners quite difficult. The physics engine won't make the outcome that much more random or unpredictable either, and frankly it often doesn't matter. A human player will still see randomness even when the RNG used is not actually random (take XorShift for example), and most certainly won't be able to predict the results.
I would recommend you simply make your spinner start spinning really fast, calculate an RNG probability, and then snap to that part of the spinner and continue to spin a certain number of rotations, slowing down as it goes, before doing some easing to make the spinner appear to come to a gentle stop or snap into place.
The fact is, in reality, most spinners in videogames tend to know exactly what they will land on before they even start spinning. This means that they can be run for specific probabilities, making game rules more explicit, as well as preventing players from being able to skip past them, as the spinner itself is just a pretty face for the outcome that has been already been determined.
Using the physics engine has no real benefit here, and I would advise writing the spinner's behaviour explicitly. The result will be easier for you to control, and not prone to annoying 'feels wrong' moments. My team and I call this "Mechanic like you mean it" and I would consider this a good design trait.

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

145 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I set an object's velocity to the velocity of an object that collided with it? 0 Answers

Object passes through collider even when isTrigger is turned off. 2 Answers

Collision detection problem 0 Answers

Collision point problem 1 Answer

Collision Killing CPU/FPS/Performance - Suggestions?! 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