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 alexsoti · Jul 01, 2019 at 03:14 PM · scripting problemcollidersphysics2dphysics 2d

How to make Two colliders, don't collide, but still be able of interact with each others

Hi, i'm currently developping a game with unity3d (2019.3 version). My problem actualy, is that I don't know how to make two colliders don't collide with each other, but still be able to interact.

Okay, let's get an example. Let's say that I have a zombie and a player, I want the player going through the zombie, but i want the collider of the player able of being detected by the one on the zombie, so the the zombie can attack the player, same for the bullets that the player shoot on the player. (little precision, the zombie attack the player thanks to a trigger collider in front of him).

I've already tried to modify things in the layer collision matrix, but the player and zombie aren't able to interact. So that's not good.

So I came here to find a little bit of help :) (ps : sorry for my bad english),Hi, i'm currently developping a game with unity3d (2019.3 version). My problem actualy, is that I don't know how to make two colliders don't collide with each other, but still be able to interact.

Okay, let's get an example. Let's say that I have a zombie and a player, I want the player going through the zombie, but i want the collider of the player able of being detected by the one on the zombie, so the the zombie can attack the player, same for the bullets that the player shoot on the player. (little precision, the zombie attack the player thanks to a trigger collider in front of him).

I've already tried to modify things in the layer collision matrix, but the player and zombie aren't able to interact. So that's not good.

So I came here to find a little bit of help :) (ps : sorry for my bad english)

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
2

Answer by HAMINPANTSdotEXE · Jul 01, 2019 at 03:52 PM

Not 100% sure if this is a feature in Unity2D (It should), but all colliders have a small box in their option menu called "Is Trigger". If you check that box on even one of them, then the collider will no longer collide with anything.


If I was you, I would leave the player with just the normal collider to make scripting easier. Then on the zombie, if you still want the zombie to have collision detection, put 2 Box Colliders 2D on it. Check the "Is Trigger" box on one of them.


KEEP READING!!!

 using UnityEngine;
 
 public class Zombie : MonoBehaviour {
     // These are most likely where your variables are
     public bool isTouchingPlayer; // This variable is for detecting when the zombie touches the player
 
 
     // Start is called before the first frame update
     void Start() {
         // Your stuff here...
     }
 
     // Update is called once per frame
     void Update() {
         // Your stuff here...
     }
 
     // NEW STUFF
     private void OnTriggerEnter2D(Collider2D collider) { // This is called when the zombie touches the player
         // Dont forget you need to TAG your player as "Player" for this to work!
         if(collider.tag == "Player") { // This line of code checks to see if the collider it hit is the player!
             // Do stuff here *1
             isTouchingPlayer = true; // This line of code is just an example of what you can do This will make the variable detecting if the zombie touched the player TRUE
         }
     }
     private void OnTriggerExit2D(Collider2D collider) { // This is called when the zombie leaves the player IMPORTANT!!!
         // Basically the same as above
         if(collider.tag == "Player") {
             isTouchingPlayer = false;
         }
     }
 }
 



Something very important to note is at the bottom you will see that there is a *1

Because we are using this from the zombies script, you cant change stuff in the player's script. There are 2 work arounds to this.

A) Use the OnTriggerEnter2D on player object instead

B) Do a bit of extra coding (Ill show an example keep reading ;))

I personally would use B. This is how


You want to make a GameObject variable called "player" and add this short line of code to your update function.

 // This line of code basically gives you access to the PUBLIC variables of the player script
         // Something very important to note is wherever it says "Player" That is where the name of your player's script should go! NOT PLAYER!!!
         Player playerScript = playerGameObject.GetComponent<Player>();
 
         // Here you can go nuts and do whatever when the zombie touches the player
         if (isTouchingPlayer) {
             // For the sake of this example ill just reduce the player's HP
             playerScript.playerHP -= 1;
         }

I've used this method a lot, i hope this helps and the explanation wasn't too confusing. :)

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 alexsoti · Jul 14, 2019 at 05:11 PM 0
Share

Hey ! thanks a lot for the time you took to help me, i'll try all of this soon.

avatar image HAMINPANTSdotEXE · Jul 15, 2019 at 02:24 AM 0
Share

no problem glad to help :D

avatar image
0

Answer by afakul · Sep 22, 2020 at 04:38 PM

still so hard to figure it out how to do ,,dang

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

191 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

Related Questions

How do I make an object rotate gradually using Rigidbody2D.MoveRotation() 3 Answers

groundCheck in small beginner project not working properly 0 Answers

[SOLVED] Physics2D OverlapCircle 2 Answers

I want to make the gameobject rigidbody.iskinematic=true for 3s and immune to damage when it triggers the collider and then false again 0 Answers

Colliders 2d apparently touch each other even if they should not. 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