• 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
Question by mealone · Jul 20, 2014 at 03:41 PM · collision

check if gameobjects overlap when collision occurs help?

Hi everyone. Can someone help me with a JavaScript sample of how to check if one Game object prefab overlaps another At the point that a collision occurs?

Eg. Gameobject A,B, and C are overlapping each other in a 2d scene, and then game object D is moved in the direction and collides with game object A w$$anonymous$$le it overlaps with B and C. Game object A,B, and C are all destroyed and a special effects prefab is instantiated at the point/position of the collision. Can anyone help me with coding t$$anonymous$$s pseudo script concept?

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by CScherer549 · Jul 20, 2014 at 05:57 PM

alt text

Is t$$anonymous$$s what you mean? What collision are you wanting to look for? You want an object and anyt$$anonymous$$ng it's overlapping to be destroyed on collision? You Also say 'point', do you mean the point in space the collision happens or the point in time?

if that's so I would try somet$$anonymous$$ng like t$$anonymous$$s.

 //first add a OnCollisionEnter method to the script attached to the object you want to check for collisions. That will notify you when it collides with somet$$anonymous$$ng and what it $$anonymous$$t. I would write a seperate script/method that checks for whether your object is overlapping stuff.
 
 function OnCollisionEnter2D(Collision col){
        if(the object you collided with has a certain tag e.g. "projectile"){
           do stuff then check for t$$anonymous$$ngs the struck object overlaps      
           checkForOverlap(col.gameObject);
        }
        //i would do it that way so you can use colliders to detect overlap and also for the initial collision
 }
 
 GameObject[] : function checkForOverlap(GameObject){
      //In here you could find all the objects the initial object is overlapping also using colliders. In t$$anonymous$$s case I would probably 
 }

Let me know if you want clarification or if t$$anonymous$$s helps at all!


qaref.png (1.6 kB)
Comment

People who like this

0 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 mealone · Jul 20, 2014 at 10:52 PM 0
Share

thank you so much for your response. Is your code in C++? Could you clarify a bit, or try javascript? I am not very versed on c++ just as yet. Thanks in advance.

avatar image CScherer549 · Jul 22, 2014 at 05:28 AM 0
Share

It should be javascript. Something like this

 import UnityEditor;
 import System.Collections.Generic;
 
 //This would hold all the objects overlapping with the one this script is attached to
 var overlapping : ArrayList
 // Use this for initialization
 function Start () {
 }
 
 // Update is called once per frame
 function Update () {
 }
 
 //This is refering to the inital collision between two objects, a projectile and a player or environment object for example.
 function OnCollisionEnter2D(col: Collision2D){
 //Here you would probably want to check what you hit, you don't want the same actions happening to everything you shoot. 
 //Walls wouldn't explode when you shot them for example
        if(the object you collided with has a certain tag e.g. "projectile"){
        
           do stuff then check for things the struck object overlaps      
           checkForOverlap(col.gameObject);
        }
        //i would do it that way so you can use colliders to detect overlap and also for the initial collision
 }
 
 function OnCollisionStay2D(col: Collision2D){
     //This method checks all objects that remain collided with the object the script is attached to.
     //Anything inside this object is added to the list, you should look into making sure the object being added
     //is unique and add it only if it is
     overlapping.Add(col.gameObject)
 }
 
 function OnCollisionExit2D(col: Collision2D){
 //When an object stops colliding it is no longer overlapped, so we remove it from the list
     overlapping.Remove(col.gameObject)
 }

Let me know if there's something you don't understand, I'd be glad to clarify.

avatar image mealone · Jul 22, 2014 at 12:31 PM 0
Share

Thanks for your response again. I got this error message ---> Unknown identifier: 'checkForOverlap'. I can't seem to find it in the reference as well. Is this the code which checks the overlaps? and the ref guide suggests overlapcircleall... what do you think?

avatar image CScherer549 · Jul 22, 2014 at 05:42 PM 0
Share

That checkForOverlap line was meant to be a pseudocode suggestion. Yeah, overlapCircleAll looks great, or if your object isn't round you could use OverlapAreaAll.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Can't crash airplane into ground 4 Answers

Player rotating after wall collision 1 Answer

Rotation collision problem 1 Answer

What is the best way to script third person real-time action melee combat in C#? 0 Answers

2D Detect collisions of a 2D block only on left/right (not top/bottom) 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