- Home /
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)
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. :)
Hey ! thanks a lot for the time you took to help me, i'll try all of this soon.