Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 phiony · Jun 02, 2020 at 02:03 AM · collision3dwhile-loop

Problem with Collision (3D)

I have an magma pool in my game and i want that the player getting damage while standing inside it. The problem is the collision detection doesn't work. The while loop never get called. My Player has a sphere collider and is named "Player". Thank you for ur help :) Here is my code thats on my "magma", it has a box collider on "Is Trigger" and a rigidbody:

 public class CollisionDamage : MonoBehaviour
 {
     public bool colWithObject;
 
     void Update()
     {
         StartCoroutine(TouchDamage());
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         if(collision.gameObject.name == "Player")
         {
             colWithObject = true;
         }
     }
 
     private void OnCollisionExit(Collision collision)
     {
         colWithObject = false;
     }
 
     IEnumerator TouchDamage()
     {
         while (colWithObject == true)
         {
             Debug.Log("Damage");
             yield return new WaitForSeconds(1);
         }
     }
 }

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
1
Best Answer

Answer by KoenigX3 · Jun 02, 2020 at 07:59 AM

Starting coroutines on Update is a bad idea. At 60 FPS, you are going to start 60 coroutines per second. However, it's not the cause of your main problem.

Your while loop won't stay there forever. If the condition is false, your coroutine ends.

You can solve your problem in two ways. If your magma pool is solid, and your character needs to collide with it, and be damaged when touches it, you simply need to uncheck IsTrigger and start your coroutine from OnCollisionEnter.

 public class CollisionDamage : MonoBehaviour
 {
    Coroutine damageCoroutine;

    private void OnCollisionEnter(Collision collision)
    {
       if(collision.gameObject.name == "Player") damageCoroutine = StartCoroutine(TouchDamage());
    }
 
    private void OnCollisionExit(Collision collision)
    {
       StopCoroutine(damageCoroutine);
    }
 
    IEnumerator TouchDamage()
    {
       while(true)
       {
          Debug.Log("Damage");
          yield return new WaitForSeconds(1);
       }
    }
 }


If your character can submerge in the magma pool, you probably need to set your collider to Trigger, and use OnTriggerEnter and OnTriggerExit instead of OnCollisionEnter and OnCollisionExit. Keep in mind that those callbacks return different parameters, you can check them at UnityDocs. The rest of the code remains the same.

Comment
Add comment · 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
0

Answer by Artik2442 · Jun 02, 2020 at 08:02 AM

I think this might be because your StartCoroutine(TouchDamage()) is in the Update method. There is nothing that can stop it. You are doing a while loop into an Update method.

Its better to use:

 public class CollisionDamage : MonoBehaviour
  {
      public bool colWithObject;
      public bool canDamage;
  
      void Update()
      {
          if (colWithObject && canDamage) // If there is a collision and you can Damage you can start damaging
          {
               StartCoroutine(TouchDamage());
          }
      }
  
      private void OnCollisionEnter(Collision collision)
      {
          if(collision.gameObject.name == "Player")
          {
              colWithObject = true;
          }
      }
  
      private void OnCollisionExit(Collision collision)
      {
          colWithObject = false;
      }
  
      IEnumerator TouchDamage() // Now, you have to wait 1 seconds before recalling this function
      {
           canDamage = false;
           Debug.Log("Damage");
           yield return new WaitForSeconds(1);
           canDamage = true;
      }
  }

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 Artik2442 · Jun 02, 2020 at 08:04 AM 0
Share

It is maybe a OnTriggerEnter and OnTriggerExit you should use if your magma pool collider is trigger.

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

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

Player dies when it Hits/crushes a block 2 Answers

Player getting stuck in corners 0 Answers

Weird Collisions Between Paddle and Ball (Breakout) 0 Answers

My trigger is called twice 1 Answer

Collision on Key press 2 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