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 /
avatar image
0
Question by Spinzaku · Aug 28, 2015 at 06:18 PM · triggerscollision2dcollisiondetectiontriggering animation box collider

OnTriggerEnter2D often not called

I have a lot of collisions in my game and most of them worked well at first. But I'm noticing a lot of incidents now, where either the Player, an enemy or a projectile just moves throught an object without interacting with it.

Here's the script of an enemy for example:

 using UnityEngine;
 using System.Collections;
 
 public class WarriorBScript : MonoBehaviour {
 
     public int strenght;
     public float mspeed;
     public float direction;
     public float dir1;
     public float dir2;
     public bool legendary = false;
     WarriorRScript otherWarrior;
     public GameObject corpse;
     
     void Start () {
         SetDirection ();
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if(other.tag == "RedSoldier")
         {
             otherWarrior = other.GetComponent<WarriorRScript>();
             if(strenght <= otherWarrior.strenght)
             {
                 Instantiate (corpse,transform.position,Quaternion.identity);
                 Destroy (gameObject);
             }
         }    
         if(other.tag == "Attack" || other.tag == "AttackRed")
         {
                 if(!legendary)
             {
                 Instantiate (corpse,transform.position,Quaternion.identity);
                 Destroy (gameObject);
                 
             }
         }
         if(other.tag == "RedSpell")
         {
             if(!legendary)
             {
                 MagierRScript.redexp --;
                 Instantiate (corpse,transform.position,Quaternion.identity);
                 Destroy (gameObject);
             }
         }
         if(other.tag == "Structure" || other.tag == "BlueSoldier" || other.tag == "BlueMage")
         {
             dir1 = -dir1;
             dir2 = -dir2;
         }
     }
 
     void SetDirection()
     {
         direction= Random.Range(0,360) * Mathf.Deg2Rad;
         dir1 = Mathf.Sin(direction);
         dir2 = -Mathf.Cos(direction);
         Invoke ("SetDirection",5);
     }
     // Update is called once per frame
     void FixedUpdate () {
         transform.Translate(dir1*mspeed/100,dir2*mspeed/100,0f);
     }
 }

The GameObject executing the script and the object tagged as Structure both have a rigidbody2d and a collider2d with onTrigger enabled.

I also tried moving the Trigger effect to a script on the structure but about every 3rd collision doesn't get recognised.

 using UnityEngine;
 using System.Collections;
 
 public class StructureScript : MonoBehaviour {
 
     MagierBScript magierBScript;
     MagierRScript magierRScript;
     WarriorBScript warriorBScript;
     WarriorRScript warriorRScript;
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if(other.tag == "BlueMage")
         {
             magierBScript = other.GetComponent<MagierBScript>();
             magierBScript.mspeed = - magierBScript.mspeed;
             magierBScript.Invoke("revertMspeed",0.1f);
         }
         if(other.tag == "RedMage")
         {
             magierRScript = other.GetComponent<MagierRScript>();
             magierRScript.mspeed = - magierRScript.mspeed;
             magierRScript.Invoke("revertMspeed",0.1f);
         }
         if(other.tag == "BlueSoldier")
         {
             warriorBScript = other.GetComponent<WarriorBScript>();
             warriorBScript.dir1 = - warriorBScript.dir1;
             warriorBScript.dir2 = - warriorBScript.dir2;
         }
         if(other.tag == "RedSoldier")
         {
             warriorRScript = other.GetComponent<WarriorRScript>();
             warriorRScript.dir1 = - warriorRScript.dir1;
             warriorRScript.dir2 = - warriorRScript.dir2;
         }
     }
 }

I've also read, that the rigidbody of nonmoving object starts sleeping after some time, and tried to prevent them from doing so with the following code, but the problem still occured,

 using UnityEngine;
 using System.Collections;
 
 public class WakeUp : MonoBehaviour {
 
     Rigidbody2D scriptBody;
     
     void Start() {
         scriptBody = gameObject.GetComponent<Rigidbody2D>();
     }
     void Update(){
         if (scriptBody.IsSleeping())
             scriptBody.WakeUp();
     }
 }

I really hope, that someone on here knows how to improve the consistency of the OnTriggerEnter2D or how to handle collisions otherwise.

Thank you for your time and for possible answers.

Comment
Add comment · 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 Spinzaku · Aug 28, 2015 at 07:36 PM 0
Share

I tested around some more and found out that the collision weren't registered at points, where one moving object collides with a point, where the colliders of two structures met. It also happens a lot if there is a corner and the object collides with two instances of the same collider at the same time. I guess this could also be a fault of my script, since it simply reverts the speed or direction twice and the object therefore just advances forward.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Happy-Zomby · Aug 28, 2015 at 06:26 PM

Hi, I encountered this problem with 3d trigger entry and the solution i found was to use ontriggerstay, on triggerexit and a boolean

 function OnTriggerStay (other : Collider)
 {
 if(other.gameObject.tag == "Tree")                                
     {
     canGatherWood = true;                                                                    
     }
 }
 
 function OnTriggerExit(other : Collider)
 {
 if(other.gameObject.tag == "Tree")
     {
     canGatherWood = false;                                        
     }
 }

then I linked a function to the change of boolean... or you could just check the boolean on the trigger stay and run something

     function OnTriggerStay (other : Collider)
     {
     if(other.gameObject.tag == "Tree" && canGatherWood == false)                                
         {
         canGatherWood = true;
         DoSomething();                                                                    
         }
     }
     
     function OnTriggerExit(other : Collider)
     {
     if(other.gameObject.tag == "Tree")
         {
         canGatherWood = false;                                        
         }
     }

hope that helps somewhat.... make sure to change the boolean to false if you destroy the collider

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

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

OnTriggerExit2D not working, but OnTriggerEnter2D does??? 5 Answers

I need help with collision detection in my 2D chess project 0 Answers

Bullet collision for space shooter - colliders, rays, etc? 2 Answers

How to activate all GameObjects named ,,GroundCollider'' by touching ,,GroundTriggerCollider'' and deactivating when no longer touching ''GroundCollider"? 1 Answer

Get callback when trigger enabled inside other collider 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