Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 beatsbychalo · Nov 15, 2020 at 12:16 PM · collisionphysicstriggertriggerscollisions

Ignore collisions with self but still trigger with self

I have a script which instantiates a bunch of objects called SPHERE. They are on a ballLayer which prevents them from colliding with each other, but they can still collide with the walls which are on the default layer. What I want is for them to still be able to trigger with each other as well. I'm not sure how I could go about this because if i set their collider to istrigger, then they no longer work with the unity physics engine. Any help would be appreciated.

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 Zubzuke · Nov 15, 2020 at 01:41 PM 0
Share

Well, remove the layer then. What's the point in preventing them to collide with each other if you want to collide with each other? As for isTrigger, instead of doing the physics collision, it sents messages so you can deal with it manually:

https://docs.unity3d.com/ScriptReference/Collider-isTrigger.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by LadyOfTheMoon · Nov 16, 2020 at 06:17 PM

You could try adding a child object the sphere. Give the child object an identical collider but enable isTrigger and set it to the default layer.

Then you can check for triggers on the child object while collisions are handled by the parent.



The alternative solution would be to check if the ball is inside anything manually using Physics.OverlapSphere every FixedUpdate. Something like this:

 void FixedUpdate()
 {
     Collider[] colliders = Physics.OverlapSphere(rigidbody.position, radius, ballLayer);
     if (colliders.Length > 1)
     {
         // More than one ball is inside the area, meaning this ball is overlapping another
     }
 }
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 beatsbychalo · Nov 16, 2020 at 09:18 PM 0
Share

if isTrigger is enabled on the child object, then wouldn't it just phase through the walls? or would I set it's position to the parent objects position every fixed update? Does OverlapSphere work if the balls are on a layer in which they don't collide with eachother? or does the OverlapSphere just check for any collider on any layer?

avatar image LadyOfTheMoon beatsbychalo · Nov 16, 2020 at 10:00 PM 0
Share

Child objects move with their parent so you don't need to update it's position manually, it won't pass through the wall because the parent will collide with it. I made a quick setup like this and it seems to work:

https://ibb.co/2cC3XrY

https://ibb.co/k8dx9Pk


OverlapSphere should work regardless of collision settings, it just returns a list of colliders that overlap in the given area, you can search for colliders on a specific layer or across all layers.

avatar image beatsbychalo LadyOfTheMoon · Nov 16, 2020 at 11:08 PM 0
Share

Okay thanks a lot, I have a lot of objects and they are all gonna have to make this check so which do you think would be less intensive?

Show more comments
avatar image
0

Answer by $$anonymous$$ · Nov 16, 2020 at 09:50 AM

Use Physics.OverlapCircle and then use a Rigidbody.AddForce once collided.

Here Check this out for Physics.OverlapCircle And this for Rigidbody.AddForce

Comment
Add comment · Show 2 · 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 beatsbychalo · Nov 16, 2020 at 05:57 PM 0
Share

Could you elaborate a bit on that? would the balls need to be able to collide for overlapCircle to work?

avatar image $$anonymous$$ beatsbychalo · Nov 17, 2020 at 12:30 AM 0
Share
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BulletCollisionScript : $$anonymous$$onoBehaviour {
 
     public float radius;
     public Layer$$anonymous$$ask whatIsTheBulletLayer;
     public Collider2D[] collidedCols;
 
     private Rigidbody2D rb;
 
     void Start(){
         rb = GetComponent<Rigidbody2D> ();
     }
 
     void Update(){
         // Gets all the bullets in the circle
         collidedCols = Physics2D.OverlapCircleAll (transform.position, radius, whatIsTheBulletLayer);
 
         if (collidedCols.Length > 1) {
             // Once the CollidedCol.Length > 1
             // Which means the bulletCollidedWithSomething
             // This Code will run
         }
     }
 
     void OnDrawGizmosSelected(){
         Gizmos.color = Color.green;
 
         // Helps you see the radius of the circle in the inspector
         Gizmos.DrawWireSphere (transform.position, radius);
     }
 }

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

283 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Multiple triggers/collisions 1 Answer

Problem: One Trigger activating a seperate Trigger it is not in contact with. 1 Answer

OnTriggerEnter2D not Working 0 Answers

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

How to design an Archer Board Scoring System 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