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 Xercium · May 04, 2016 at 03:06 PM · collisionphysics2d gametrigger2d-physics

How can I make sure triggers or collisions are never skipped even for high speeds?

Firstoff: I once made a forum post that handelled a simular problem and I also added this to it, but I'm not getting any reactions so I'm just going to refer to it here. (it may be eassier to read it over there. Forum post

I have 2 types of bullets and the problems are I think simular in solution but the effect is somewhat different.

My first bullet type is a fireball, it just moves in straight lines, when colliding with a player it is destroyed unless it's the player who fired the bullet, if it collides with another bullet or object or ground it should be destroyed aswel. Now for some reason 2 colliding bulelts arn't always detected, 2/3 times they get destroyed, 1/3 they just continue.

GIF: Example of the error

I just noticed that if I try hard enough I can even pass through walls: GIF: Another thing that goes wrong

This is my collision matrix: ​IMG: Collision Matrix

The code that sees if any trigger has been made:

 using UnityEngine;
 using System.Collections;
  
 /// <summary>
 /// Stef Geukens
 ///
 /// This code will determine if the object it is attached to has triggered a overlap with a collider object.
 /// If so, the object is destroyed. When the object hits another player then his own (set playernum to the correct player)
 /// then a function is called (Hurt) in the ShotPlayer script so thaty script knows when the score in the scorescript has
 /// to be updated and for what player.
 /// </summary>
  
 public class BulletTriggerDetection : MonoBehaviour {
     public static BulletTriggerDetection BTD;
     public int playerNum = 1;
     public bool destroyOnImpact = true;
     public bool destroyOnTimer = true;
     public bool destroyOnNumberOfCollides = false;
     public int maxNumberOfCollides = 6;
     private int numberOfCollides = 0;
  
     void Start()
     {
         BTD = gameObject.GetComponent<BulletTriggerDetection>(); ;
     }
  
     void OnTriggerEnter2D(Collider2D  col)
     {
       Debug.Log(gameObject.name + " has collided with " + col.gameObject.name);
       Debug.Log("My layer: " + gameObject.layer);
       Debug.Log("Number of Impacts: " + numberOfCollides);
  
         if (playerNum == 1)
         {
        
                 if (col.gameObject.tag == ("Player2") || col.gameObject.tag == ("Player3") || col.gameObject.tag == ("Player4"))
                 {
                     col.gameObject.GetComponent<ShotPlayer>().Hurt(col.gameObject.tag, playerNum);
                     // Debug.Log("Player" + playerNum + " just shot " + col.gameObject.tag);
                     destroyBullet(gameObject, 0, true, destroyOnTimer, destroyOnNumberOfCollides);
                 }
                 else if (col.gameObject.tag != ("Player1") && destroyOnImpact)
                 {
                     destroyBullet(gameObject, 0, true, destroyOnTimer, destroyOnNumberOfCollides);
                 }
                 else if (col.gameObject.tag != ("Player1"))
                 {
                     numberOfCollides += 1;
                     destroyBullet(gameObject, 0, true, destroyOnTimer, destroyOnNumberOfCollides);
                 }
  
  
         }
         else if (playerNum == 2)
         {
  
                 if (col.gameObject.tag == ("Player1") || col.gameObject.tag == ("Player3") || col.gameObject.tag == ("Player4"))
                 {
                     Debug.Log("State1");
                     col.gameObject.GetComponent<ShotPlayer>().Hurt(col.gameObject.tag, playerNum);
                     // Debug.Log("Player" + playerNum + " just shot " + col.gameObject.tag);
                     destroyBullet(gameObject, 0, true, destroyOnTimer, destroyOnNumberOfCollides);
                 }
                 else if (col.gameObject.tag != ("Player2") && destroyOnImpact)
                 {
                     Debug.Log("State2");
                     destroyBullet(gameObject, 0, true, destroyOnTimer, destroyOnNumberOfCollides);
                 }
                 else if (col.gameObject.tag != ("Player2"))
                 {
                     Debug.Log("State3");
                     numberOfCollides += 1;
                     destroyBullet(gameObject, 0, true, destroyOnTimer, destroyOnNumberOfCollides);
                 }
  
         }
         else if (playerNum == 3)
         {
  
  
                 if (col.gameObject.tag == ("Player1") || col.gameObject.tag == ("Player2") || col.gameObject.tag == ("Player4"))
                 {
                     col.gameObject.GetComponent<ShotPlayer>().Hurt(col.gameObject.tag, playerNum);
                     // Debug.Log("Player" + playerNum + " just shot " + col.gameObject.tag);
                     destroyBullet(gameObject, 0, true, destroyOnTimer, destroyOnNumberOfCollides);
                 }
                 else if (col.gameObject.tag != ("Player3") && destroyOnImpact)
                 {
                     destroyBullet(gameObject, 0, true, destroyOnTimer, destroyOnNumberOfCollides);
                 }
                 else if (col.gameObject.tag != ("Player3"))
                 {
                     numberOfCollides += 1;
                     destroyBullet(gameObject, 0, true, destroyOnTimer, destroyOnNumberOfCollides);
                 }
  
         }
         else if (playerNum == 4)
         {
  
  
                 if (col.gameObject.tag == ("Player1") || col.gameObject.tag == ("Player2") || col.gameObject.tag == ("Player3"))
                 {
                     col.gameObject.GetComponent<ShotPlayer>().Hurt(col.gameObject.tag, playerNum);
                     // Debug.Log("Player" + playerNum + " just shot " + col.gameObject.tag);
                     destroyBullet(gameObject, 0, true, destroyOnTimer, destroyOnNumberOfCollides);
                 }
                 else if (col.gameObject.tag != ("Player4") && destroyOnImpact)
                 {
                     destroyBullet(gameObject, 0, true, destroyOnTimer, destroyOnNumberOfCollides);
                 }
                 else if (col.gameObject.tag != ("Player4"))
                 {
                     numberOfCollides += 1;
                     destroyBullet(gameObject, 0, true, destroyOnTimer, destroyOnNumberOfCollides);
                 }
  
       }
     }
  
  
     public static void destroyBullet(GameObject objectInNeedOfDestruction, float destroyTimer, bool hit, bool exDestroyOnTimer, bool exDestroyOnCollide)
     {
         Vector3 position = objectInNeedOfDestruction.transform.position;
         Quaternion rotation = objectInNeedOfDestruction.transform.rotation;
         if (BTD.destroyOnTimer && exDestroyOnTimer)
         {
             Destroy(objectInNeedOfDestruction, destroyTimer);
             BTD.showBulletAnimation(position, rotation, hit);
         }
         else if (BTD.destroyOnNumberOfCollides && BTD.numberOfCollides >= BTD.maxNumberOfCollides)
         {
             Destroy(objectInNeedOfDestruction, 0);
             BTD.showBulletAnimation(position, rotation, hit);
         }
     }
  
  
     public Transform hitPrefab;
     void showBulletAnimation(Vector3 position, Quaternion rotation, bool hit)
     {
         if (hit)
         {
             Transform hitParticleClone = Instantiate(hitPrefab, position, rotation) as Transform;
             Destroy(hitParticleClone.gameObject, 2f);
         }
  
     }
 }

 

gameobject settings (rigidbody is set to continues): ​IMG: Setting Fireball


The second type of bullet is a rubber ball, effected by gravity and it should bounce of objects and ground, and get destroyed by other bullets or hitting and killing a player.

​IMG: Settings Rubberball

As you can see this parent has been set as a trigger as to be destroyed by a player or another bullet, the child has been set as collider: ​IMG: More information about the Rubberball

​GIF: Showing the problem with the rubber ball

With very low speeds the rubber ball seems to work a bit beter but thats not what I want. GIF: Showing it works on low speed

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

Answer by korbul · May 04, 2016 at 05:39 PM

Hello,

The quick thing you can try, is to play with your Physics2d settings where the collision matrix is.

You can find what each setting does here http://docs.unity3d.com/Manual/class-Physics2DManager.html

For starters, you may want to increase Velocity Iterations and Position Iterations but the other settings may help you as well.


Now for the off-topic part.

I looked at your code and there is quite a lot of duplication just for checking

if (playerNum == 1) else if (playerNum == 2) etc ...

This does not scale well with more players. Imagine if at some point, you want to add 2 more players. You would have to change a lot of code.

My suggestion is to tag all players with the tag "Player". Just "Player", no number after it. Now your OnTriggerEnter2D becomes

 void OnTriggerEnter2D(Collider2D  col)
 {
     Debug.Log(gameObject.name + " has collided with " + col.gameObject.name);
     Debug.Log("My layer: " + gameObject.layer);
     Debug.Log("Number of Impacts: " + numberOfCollides);
     
     if(col.gameObject.tag == ("Player"))
     {
         BulletTriggerDetection otherBTD = col.GetComponent<BulletTriggerDetection>();
         if(playerNum != otherBTD.playerNum)
         {
             col.gameObject.GetComponent<ShotPlayer>().Hurt(col.gameObject.tag, playerNum);
             // Debug.Log("Player" + playerNum + " just shot " + col.gameObject.tag);
             destroyBullet(gameObject, 0, true, destroyOnTimer, destroyOnNumberOfCollides);
             numberOfCollides += 1;
         }
     }
     else if(destroyOnImpact)
     {
         destroyBullet(gameObject, 0, true, destroyOnTimer, destroyOnNumberOfCollides);
     }
 }
Comment
Add comment · Show 4 · 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 Xercium · May 04, 2016 at 06:48 PM 0
Share

Thanks, you've been a great help! I'm still learning what it is I can do with Unity and there is allot to learn!

Today I learned something new, thank you ^^

avatar image Xercium · May 04, 2016 at 07:09 PM 0
Share

It seems I was to fast to react, I quickly looked at it and basics seem to work, but then I started shooting my opponent and the bullets go straight through. (That was also the initial reason for my duplication :p)

avatar image korbul · May 04, 2016 at 08:05 PM 0
Share

Hey, I have no doubt that what I suggested would work. The code above was written without knowledge of your whole code. You need to adjust some things for it to work properly.

Also, the duplication removal i suggested was not a fix for bullets going trough objects. It's just a suggestion

avatar image Xercium · May 04, 2016 at 09:08 PM 0
Share

Hi, yeah I know that the duplication part wasn t a solution to my original question. But now my other player don't get triggered, so your code isn t a valid alternative for my code, I lokken at it but I can't really find a solution for this. But I will look if I can get the duplication out

avatar image
0

Answer by coolraiman · May 04, 2016 at 07:27 PM

if your bullet actually go as fast as a real bullet (higher than the speed of sounds), Use raycasting. This way you are sure to miss nothing. only down side with raycasting, it does not work well for large object.

Comment
Add comment · Show 3 · 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 Xercium · May 04, 2016 at 07:43 PM 0
Share

It's a 2D platformer and the bullets are slow, you can dodge them if you want. So I didn't think it would be to fast

avatar image coolraiman · May 04, 2016 at 07:48 PM 0
Share

is the Is Trigger on your collider on? trigger function only work if you checked the is trigger

avatar image Xercium · May 04, 2016 at 09:09 PM 0
Share

Yeah, it is checked.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D collision not registering 1 Answer

Trigger2D not working 0 Answers

collider doesnt work the 2nd time 1 Answer

Unity 2D Collider Safe Sizes 0 Answers

overlapsphere to destroy NPCs on exit 1 Answer


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