I’m making a 2D platformer, and my player can shoot projectiles. I don’t want the projectiles to be able to collide with the player, have tried some different things, but can’t get it to work. Can anyone tell me how to accomplish this?
For 2D, you need to put the objects on separate layers. Then you can either set the collision matrix in:
Edit > Projects Settings > Physics2D
Or you can use Physics2D.IgnoreLayerCollision()
More info on the Physics2D Manager:
https://docs.unity3d.com/Documentation/Components/class-Physics2DManager.html
It’s very simple.
Simply take the object(s) that you want to be ignored, and give them a tag, or layer. Now the method of exactly “how” to ignore them is different based on what you are doing, but all you need to do is tell the object that the script is attached to (the one you want to be the “ignorer”, to do so when it encounters any object with the tag, or layer you set up.
If using a tag:
void OnCollisionEnter(Collider collision)
if(collision.gameObject.tag == “theobjectToIgnore” )
If using a layer:
void OnCollisionEnter(Collider collision)
if(collision.gameObject.layer == “theobjectToIgnore” )
and I would make sure that object to be ignored has a rigid body.
TAG void OnCollisionEnter(Collision collision) { if (collision.gameObject.tag == "theobjectToIgnore") { Physics.IgnoreCollision(theobjectToIgnore.collider, collider); } LAYER void OnCollisionEnter(Collision collision) { if (collision.gameObject.layer == "theobjectToIgnore") { Physics.IgnoreCollision(theobjectToIgnore.collider, collider); } : )
– BryologyHow would one start the script for the following? I'm not great at code and have tried finding the answer but have had no luck. Any help with that would be very much appreciated! void OnCollisionEnter(Collision collision) { if (collision.gameObject.tag == "theobjectToIgnore") { Physics.IgnoreCollision(theobjectToIgnore.collider, collider); }
– CharlenecharleroiThis is not a solution. Calling IgnoreCollision in OnCollisionEnter does not cancel the initial collision, so objects still receive collision impulses.
– ConcernThanks man, works perfectly. using UnityEngine; public class playerMovementHorizontal : MonoBehaviour { public Rigidbody2D rb2d; public float speed; private void Update() { rb2d.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, rb2d.velocity.y); Debug.Log(transform.position); } private void OnCollisionEnter2D(Collision2D col2d) { if (col2d.collider.name == "GroundCheck") { Physics2D.IgnoreCollision(col2d.collider, col2d.otherCollider); } } }
– GerlachovkaSo I have tried the following as suggested.
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "theobjectToIgnore")
{
Physics.IgnoreCollision(theobjectToIgnore.collider, collider);
}
I’m trying to push a ball through a barrier, the ball is supposed to go through but not the character. The object is named Ball and the tag is also named Ball. So my script is as follows:
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Ball")
{
Physics.IgnoreCollision(**Ball**.collider, collider);
}
}
the second ball though is red and it is not being detected via typing… can anyone help me?
The second Ball should be collision. Assuming this code is on the barrier, the OnCollisionEnter declaration is grabbing the Ball as an object named "collision". The line should be: Physics.IgnoreCollision(collision.collider, collider);
– ExhibitorYou could use the Layer Collision Matrix from Edit > Project Settings > Physics to do that without scripting.
Create a layer (e.g Layer1) and disable the Layer1/Layer1 collision. Also, assign your objects to this layer, of course.
Suppose that i have few objects outside a huge box. The box and all the objects have collider components on them. I want those objects to be able to enter the box (without any change to their velocity) and once they enter, they should get collided back into the box whenever they collide with the huge box. I.e. i want to ignore the first collision so that objects can enter. How can i achieve this?
– Carmarthenshireholy heck tysm man
– TumbledownPerfect, works as @Stegodon says.
– DiamondFor detecting and comparing two layers at runtime in a collision detector, I wrote the following extension:
public static class UnityExtensions {
public static bool ContainsLayer(this LayerMask mask, int layer) {
return ((1 << (int)layer) & (int)mask) > 0;
}
}
You can then use that in a script with a collision handler like so:
using UnityEngine;
public class MyCollider : MonoBehaviour {
[SerializeField] private LayerMask m_Layers;
// should work for ANY trigger or collision detector.
private void OnTriggerEnter2D(Collider2D other) {
if (m_Layers.ContainsLayer(other.gameObject.layer)) {
// ignore collision or whatever you want.
}
}
}
From there, attach the script to your collider and then, in the editor, populate the layers field through the layers dropdown and you’re good to go!
The Collision Ignoring aspect of this question has been pretty well covered, I think, and it has been really helpful in its own right. I would just like to point out that, for the purpose of making sure a bullet/projectile doesn’t interfere with your player upon firing, you could originate the bullet from slightly in front of the player (or slightly outside the player’s space as appropriate to the direction of travel). If you have situations in which the player could come back in contact with the bullet, the various collision ignoring methods here should handle it.
You are not supposed to put the IgnoreCollision on OnCollisionEnter as it will still detect the first ever collision, but you can put it on the start() function like this:
private void Start()
{
GameObject player = GameObject.FindGameObjectWithTag("Player");
Physics2D.IgnoreCollision(player.GetComponent<Collider2D>(), GetComponent<Collider2D>());
}
So, first you assign an object to the variable player, here I’m assigning it with the object with the tag “Player” then after that you just use the IgnoreCollision function like usual.
Simple, concise, extensible. Thanks, just what I needed to find!
– InvokeThis works perfect, thanks.
– Uhfthat worked perfectly. thank you so much [iqbalklv]
– Polashmy recommendation would be to set individual layers for specific objects and then use layers to ignore each other. (so like default being 0 and transparent being 1 ect)
for 3D:
Physics.IgnoreLayerCollision(9, 10);
for 2D:
Physics2D.IgnoreLayerCollision(9, 10);
just switch out 9, 10 for the 2 layers you want to ignore each other.
Set Game Objects Collider “isTrigger” to true and use:
private void OnTriggerEnter2D {
if (collision.gameObject.name.Contains("ObjectToRemove")) {
print("ObjectToRemove Collided");
}
}
You can also use IgnoreLayerCollision.
Physics.IgnoreLayerCollision(8, 10);
This would ignore collisions between layers 8 and 10.
Just make sure that when you are making 2D game you are using
Physics2D.IgnoreLayerCollision(8, 10);
© 2022 - 2024 — McMap. All rights reserved.
Have tried to use Physics.IgnoreCollision, it doesn't work. The projectiles still collide with the player. Here's the part of the script. No errors or anything. void OnCollisionEnter2D(Collision2D coll) { if (coll.gameObject.tag == "projectile") { Transform bullet = Instantiate(bulletPrefab) as Transform; Physics.IgnoreCollision(bullet.collider, collider); } And have no idea of how the matrix you mentioned works
– PathfinderNo bulletPrefab is a GameObject. How do i assign it as a Transform? (i'm guessing it has to be the transform of the projectiles). But the script is on the same object as the player collider yeah.
– PathfinderNo. It did not work. The same thing as before; the player collides with the projectiles.
– PathfinderI should have looked at your code or read your question more carefully. IgnoreCollision() is for 3D colliders. You are making a 2D system. From a look at the reference (not by experience), it appears that you will have to put the bullets on a different layer. You can then use [Physics2D.IgnoreLayerCollision()][1]. Sorry for not getting it right up front. You can also set what layers collide using: Edit > Project Settings > Physics2D [1]: http://docs.unity3d.com/Documentation/ScriptReference/Physics2D.IgnoreLayerCollision.html
– KilloughYES! It works. Using the matrix to ignore collision between the two layers. Thanks for help.
– Pathfinder