I thought that trigger colliders were not meant to interact with each other, I have the following game object:
(The "AttackArea Game Object is a child of a "Player" Game Object)You can see I set it to be a trigger, the script "Attacks" only has the following code:
public int attackStrength;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.GetComponent<Health>() != null && collision.CompareTag("Enemy"))
{
Debug.Log("touched: " + collision.name);
Health health = collision.GetComponent<Health>();
health.ReduceHealthPoints(attackStrength);
}
}
The OnTriggerEnter2D method is getting called when this Polygon collider touches a Circle collider also set as a trigger:
Not sure why the two trigger colliders are interacting at all, I thought that wasn't possible.
These are the RigidBody settings on the game object that has the Polygon Collider 2D (it's parent):
With the isTrigger property set to true, the collider will not collide, but it will send trigger events.
If you do not want to interact with certain game objects you can add tags to the relevant game objects and compare the tags.
For example:
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("CircleCollider")) return;
}
Related
In Unity 5.6 C#, I know there is a way to check for if a collider is being touched by any other collider by using IsTouching.
However, I would like to know how to group two colliders together(that are touching each other), and how to check if they both are touching any collider besides each other.
I will give it a shot with the idea I mentioned in the comments (I see it is hard to understand with only the comments section).
I would use a list of collisions and store any touches here, filtering out the "partner" collider using OnCollisionEnter and OnCollisionExit.
Since both are attached to the same GameObject it is easy to filter them:
public class Collisions : MonoBehaviour
{
// Show in the Inspector for debug
[SerializeField] private List<Collider> colliderList = new List<Collider>();
public bool IsTouching => colliderList.Count != 0;
private void Awake ()
{
// Make sure list is empty at start
colliderList.Clear();
}
private void OnCollisionEnter(Collision collision)
{
// Filter out own collider
if(collision.gameObject == gameObject) return;
if(!colliderList.Contains(collision.collider) colliderList.Add(collision.collider);
}
private void OnCollisionExit(Collision collision)
{
// Filter out own collider
if(collision.gameObject == gameObject) return;
if(colliderList.Contains(collision.collider) colliderList.Remove(collision.collider);
}
}
Typed on smartphone but I hope the idea gets clear
I tried using OnCollisionExit, but it doesn't detect the other object's box collider 2D being disabled (When I disable the other objects box collider 2D it doesn't detect it as stopping being collided). I need the other objects collider to be disabled because I'm using it as a range indicator for punching and I don't want it to interact with other objects(example: The player pushes away the enemy with his range indicator). Is there another method I could use?
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.tag == "PunchRange")
{
Player.GetComponent<Fight>().PunchInRange = true;
}
if (collision.collider.tag == "KickRange")
{
Player.GetComponent<Fight>().KickInRange = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.collider.tag == "PunchRange")
{
Player.GetComponent<Fight>().PunchInRange = false;
}
if (collision.collider.tag == "KickRange")
{
Player.GetComponent<Fight>().KickInRange = false;
}
}
I want it to detect whenever it is not colliding with an object even when the object's box collider 2D is disabled.
You have to restructure your introduction as it is so convoluted idk what you exactly want.
To detect collision or its lack you have to use colliders(maybe put additional collider but as a trigger). However what I understood is that you want to use them as triggers. On Collider2D component there is value isTrigger that you can set. Setting it makes the collider still work but not phisically (other colliders can pass thru it). Set that on the collider and use:
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerExit2D.html
That should work for you.
I have a couple of tilebase variables that are set using the unity inspector:
public TileBase obstacle; //obstacle tile
public TileBase spikes; //instant death tile
I'm using OnCollisionEnter2D in a script that is set to the tilemap asset.
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name == "Player")
{
PlayerPrefs.SetInt("Collision", 1);
}
}
I want to be able to detect when the player has collided with the spikes TileBase as that should trigger a game over. The asset name that is set to the spikes variable through the inspector is also called Spikes or Spikes (Tile) in the palette.
I tried using adding collision detection to a script that is used on the player asset to check what objects it collides with
void OnCollisionEnter2D(Collision2D col)
{
Debug.Log(col.gameObject.name);
}
But all it comes back with is the name of the tilemap.
First you should add Layers to your objects. An easy example is to add a "TileBase" or "Tile" Layer to your TileBase objects.
You can then allow your Player to only collide with certain things and NOT your tilemap. See the collision matrix for this.
Then alter your code like this:
void OnCollisionEnter2D(Collision2D col)
{
var tilebase = col.GetComponent<TileBase>();
if (tilebase != null)
{
Debug.Log(tilebase.gameObject.name);
}
}
I am making a 2D game. I have 2 game objects, a player, and some obstacles and I want the player object to be destroyed on collision. I have added box colliders to both the objects as well as tags but the collision is not taking place as there are no log messages in the console.
void OnCollisionEnter2D (Collision2D col)
{
Debug.Log("collision name = " + col.gameObject.name);
if (col.gameObject.tag == "cow") {
Destroy(gameObject);
}
}
As we discussed in comments:
Add Rigidbody2D component to at least one of colliding objects.
How do I determine if a sprite (Sprite1) if on top of (Sprite2)
I need this to switch levels when you touch another sprite with your sprite.
I tried:
void OnCollisionEnter2D(Collision2D other)
{
//code to run
}
But this made it so that everything it touched ran the code. :(
You should attach the script that contains oncollisioneneter to your sprite1 game object for example and in your sprite 2 game object add a tag like sp2 or whatever you want , then in your script when collision happens you check if the object that sprite one collided with is sprite 2 you check it with tag property of sprite 2 like this
void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "sp2")
{
//put your change levelcode here
}
}
Your halfway there. Right now you are running the code whenever the sprite collides with another 2D collider. What you then need to do is determine if that collider is the one attached to the particular sprite we want to trigger the code.
Tag the object containing your level exit, and check for the tag when you get a hit.
void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "levelExit")
{
Debug.Log("next level");
}
}