Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Oct 08, 2018 at 08:14 PM by Igor_Vasiak for the following reason:

I solved it by myself. The issue wasn't due to Unity, but because of my code.

avatar image
0
Question by Igor_Vasiak · Oct 01, 2018 at 10:42 PM · collisionrigidbodyproblem during runtime

OnCollisionEnter method iterating multiple times after each collision.

So, I'm running an OnCollisionEnter method in my Boss enemy to detect if a spell did hit him. It's a pretty usual thing, it never fails.


The problem here is that for each consecutive spell hit, the number of times it iterates through the collision detection increases.


It goes like this:


  • Hit 1 - 1 Hit.

  • Hit 2 - 19 Hits.

  • Hit 3 - 36 Hits.

  • ...


So... It doesn't really work for me. Each spell deals one Hit Point. This means that the Boss should die in ~20 seconds assuming one never misses a shot. Instead, what happens is that the Boss dies in ~5 seconds because of these multiplied hits. Does anyone know why is that? Here are a few details that may or may not interest you.


The Boss has a Rigidbody set for full constraints lock, and CCD (Continuous Collision Detection). He also has a Box Collider of its size with Trigger set to false. There are no other Colliders and/or Rigidbodies in its children. He never uses gravity. He never moves.


The spell has a Rigidbody with no constraints locked and CCD enabled. It has a Non-Trigger Box Collider of its size. There are no other Colliders and/or Rigidbodies in its children. It never uses gravity. It is always in movement.


Any help is profoundly appreciated. Thanks in advance.

Comment
Add comment · Show 6
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 Vicarian · Oct 02, 2018 at 03:41 PM 0
Share

Are you destroying the spell promptly on collision, or is it allowed to pass through? If it's allowed to pass through, you could store the object that hit the boss in a list and ignore it if it pops up again as a naieve solution:

 using System.Collections.Generic;
 using UnityEngine;
 
 public class Boss : $$anonymous$$onoBehaviour {
     List<GameObject> collidedObjects;

     void Awake() {
         collidedObjects = new List<GameObject>();
     }
 
     void OnCollisionEnter(Collision other) {
         if (collidedObjects.Contains(other.collider.gameObject))
             return;
 
         collidedObjects.Add(other.collider.gameObject);
         // Take damage, etc
     }
 }

In this approach, the list will eventually contain a bunch of null objects if at some point the spells are destroyed. You might want to run a coroutine to prune it occasionally. However, if you're destroying the spell on collision, you can try experimenting ins$$anonymous$$d with using the OnCollisionStay callback.

avatar image Igor_Vasiak Vicarian · Oct 03, 2018 at 06:37 PM 0
Share

I'm not destroying the Game Object since it is an unnecessarily heavy loaded operation. I use an object pooler from which I spawn the projectiles.


Once the projectile hits the boss, it is instantly deactivated to prevent issues. But apparently not all of them.

avatar image TreyH · Oct 02, 2018 at 03:49 PM 0
Share

I've never had an issue like this, can you link its collision code?

avatar image Igor_Vasiak TreyH · Oct 03, 2018 at 06:35 PM 0
Share

It's like...

 private void OnCollisionEnter (Collision collision)
 {
     if (collision.tag == "Projectile")
     {
         [...] // Updates health bar and boring stuff that ain't needed :)
         health--;
     }

     [...]
 }

avatar image Nixmortem · Oct 02, 2018 at 07:25 PM 1
Share

This doesn't really fix your problem or explain it but what if you put the OnCollisionEnter $$anonymous$$ethod on the spell? So when it collides, get the boss health and increment it down before destroying the spell object. I had a similar issue on a different type of game and that was how I got around it. I've also found OnTriggerEnter to be a bit more forgiving.

avatar image Igor_Vasiak Nixmortem · Oct 03, 2018 at 06:32 PM 0
Share

This can actually work. But I wanted to understand the issue to prevent future issues as well.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

165 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

Related Questions

How I can make gameobject detect collisions and make every other gameobject be able to go through it? 1 Answer

Collision without movement!?? 2 Answers

How can the player position updates randomly when the player isn't movingl? 0 Answers

Rigidbody: how to get collisions for the current frame ? 0 Answers

how to make an Explosive rigidbody trigger by another collision box 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