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 KiethFurry · Apr 15, 2016 at 05:00 PM · collisionspritescollisiondetection

Getting sprites to stop going through one another?

I'm trying to get the enemies in my 2D game to stop going through each other. Their "Is Trigger" is checked in order for them to harm the player. I've tried unchecking the box, which does cause the enemies to run into each other and not through each other, but it also does the same to the player and prevents the player's script for taking damage to work. Is there a way to make the enemies not pass through each other yet still be "Is Trigger", maybe in the C# script on the enemy? Thanks.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by meat5000 · Apr 15, 2016 at 05:00 PM

There's a sudden obsession with everyone using Triggers as Physics Colliders. This is nonsense practice. Triggers are area-switches nothing more.

Marking isTrigger takes away the physics barrier of the collider. This is why it can be used as an area trigger. For collisions, remove isTrigger and use OnCollision routines instead of OnTrigger.

Its all in the manual.

Could everyone just stop trying to use Triggers for Physics Collisions please?

Comment
Add comment · Show 11 · 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 meat5000 ♦ · Apr 15, 2016 at 05:02 PM 0
Share

I mean literally, the question under this one in the moderation queue was the same damn thing. Seems that someone has released a tutorial which is spreading bad info.

avatar image KiethFurry · Apr 15, 2016 at 05:14 PM 0
Share

I'm sorry, I'm in a high school game design class trying to make my end of the year project. I haven't been using Unity for long.

I have tried unchecking "Is Trigger" and using OnCollision ins$$anonymous$$d, however when I try this no collision is detected. The player still takes no damage from hitting the enemy.

avatar image meat5000 ♦ KiethFurry · Apr 15, 2016 at 05:14 PM 0
Share

You are only following half the advice.

http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

I simplified 'OnCollision' but didnt consider that you wouldnt follow. Colliders come with three routines which work when isTrigger is checked.

OnTriggerEnter

OnTriggerStay

OnTriggerExit

They come with three routines which work when isTrigger is NOT checked

OnCollisionEnter

OnCollisionStay

OnCollisionExit

Its all easily searchable in the docs and has examples in the Scripting Reference.

avatar image KiethFurry · Apr 15, 2016 at 08:20 PM 0
Share

I DID follow your advice, mainly because I tried all of those before even posting this question. The thing is that no matter what I put in, OnCollisionEnter, OnCollisionStay, or OnCollision Exit, no collision is ever detected between the enemy and the player when the enemy's "IsTrigger" is unchecked.

avatar image meat5000 ♦ KiethFurry · Apr 15, 2016 at 08:21 PM 0
Share

Any collision requires 2 colliders and 1 non-kinemtic rigidbody.

avatar image tanoshimi KiethFurry · Apr 15, 2016 at 08:36 PM 1
Share

Given that you're talking about a 2d game, you should probably be using Rigidbody2Ds, Collider2Ds, and OnCollisionEnter2D()...

avatar image meat5000 ♦ tanoshimi · Apr 15, 2016 at 08:40 PM 0
Share

I make too many assumptions :P

avatar image KiethFurry · Apr 16, 2016 at 05:35 AM 0
Share

I've BEEN using two colliders and non kinematic rigidbodies. I've ALSO tried every kind of collision code I can think of. OnCollisionEnter2D, OnTriggerEnter2D, etc. I've unchecked "IsTrigger" on the enemies and it doesn't work. I put in code for unity to output the words "Player Hit" when collision with the enemy occurs, and so far it has yet to give the message. Thus I'm assu$$anonymous$$g that the player and enemy are hitting one another, but not passing into one another even by one pixel. So the OnCollisionEnter2D doesn't work since their hit boxes never cross. I need a way to get the player to detect collision with the enemies in this way.

avatar image meat5000 ♦ KiethFurry · Apr 16, 2016 at 10:47 AM 0
Share

If the callback is not fired the collision has not occurred.

One non-trigger collider on each object and one non-kinematic rigidbody is required on either object. The collisions will occur even without using any OnCollision functions at all. Realise this routine is a bonus and is not a requirement.

If this basic setup is not working there are several things that could cause this.

1) $$anonymous$$oving objects with transform.Translate or modifying .position (causes teleportation).

2) $$anonymous$$oving objects too fast for the collision to register (yes this even happens on Cont. Dynamic)

3) Something is awry in the Collision $$anonymous$$atrix (Physics $$anonymous$$anager)

As tanoshimi says, $$anonymous$$ake sure the colliders and rigidbodys are the 2D variants!

avatar image tanoshimi KiethFurry · Apr 16, 2016 at 10:57 AM 0
Share

Please post your collision code and a screenshot of your inspector settings showing the components attached to your objects. Have you got the function signature for OnCollisionEnter2D correct?

avatar image KiethFurry · Apr 21, 2016 at 05:17 PM 0
Share

Here is the player's collision code, which it so far only uses when it's hit by the enemies.

 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.tag == "Enemy"  && !invulnerable)
     {
         print("HIT");
         invulnerable = true;
         HP--;
         Invoke("NotInvulnerable", invulnerableTime);
     }
     if(other.gameObject.tag == "BossShot" && !invulnerable)
     {
         invulnerable = true;
         HP -= 2;
         Invoke("NotInvulnerable", invulnerableTime);
     }
 }

Here is the code the enemies use for colliding with the player's laser shots, which works just fine no matter what.

 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.tag == "Projectile")
     {
         HP--;
     }
 }

alt text

This is a screenshot of the player's rigidbody and box collider.

alt text

And here is a screenshot of the enemy's rigidbody and box collider.

screenshot1.png (53.0 kB)
screenshot2.png (52.1 kB)

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

Pixel Perfection collision on 3d animated object 0 Answers

Find axis of collision 0 Answers

Checking Collision on instantiated object 0 Answers

How do I make OnTriggerEnter() respond to just one collider? 2 Answers

Unity changes the collisionDetectionMode in builds 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