I’m sure there’s an easy fix for this. I just don’t know the syntax well enough. Basically, when the bullet hits something, I need it to check that the thing it collided with has a tag called Enemy. What is the way to check for this ?
//I need to know if the collision both involves an enemy tag and the bullet tag, then increment the score.
//If you are wondering why I did it like this, well I’ve done something with the A* pathfinding script and its //really f’ed the logic behind my assignment… this is a quick easy fix so I can hand this thing in tonight. This works but increments the score when the bullet hits ANYTHING… I need it to do so only when it hits an enemy.
using UnityEngine;
public class BulletCollision : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag.Equals("bullet"))
{
// if the thing that the bullet collides with is ALSO an enemy then add
// one to the killCount.
CinemachineShake.Instance.ShakeCamera(6f, .2f);
Destroy(collision.gameObject);
Destroy(gameObject);
}
}
}```
Thanks a bunch... pulling my hair out here.
Does not work unfortunately… but it DOES work with the planet tagged objects when I change the tag to ‘planet’.
Possibly my enemy objects are not registering as ‘enemy’ despite their tags…
The line “Does this even work?” ← comes up on console but not the “It hit an enemy!”
public class BulletCollision : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag.Equals("bullet"))
{
Debug.Log("Does this even work?");
if (collision.gameObject.CompareTag("enemy"))
{
Debug.Log("It hit an enemy!");
}
// if the thing that the bullet collides with is ALSO an enemy then add
// one to the killCount.
CinemachineShake.Instance.ShakeCamera(6f, .2f);
Destroy(collision.gameObject);
Destroy(gameObject);
}
}
}
Is there a line of code I can write to identify what exactly it’s hitting?
A GameObject can only have one tag, so you can remove this if-statement: if (collision.gameObject.tag.Equals("bullet"))
This is checking if the object that the bullet collided with is also tagged as “bullet”.
If you’re trying to hit an enemy (GameObject tagged as “enemy”), this will always return false.
The other part has revealed something weird. So I tagged the enemies with ‘enemy’ but when the Debug statements flash they are listed as
name: bullet(Clone)
tag: bullet
While when I shoot anything else in the game, its tagged normally, even the boss monsters.
I’m not sure how to fix this. I’m sure your statements work… but I’m not sure where the tags are changing before the bullets hit them.
I don’t know if this is a viable solution, depending on how far along your project is, but it would be better to make use of Layers instead.
For instance, you could assign your enemy GameObjects onto an “enemy” layer, and your bullet GameObjects onto a “bullet” layer. Then in your Physics2D project settings (Edit > Project Settings > Physics2D), edit the collision matrix so that the “bullet” layer can only collide with the “enemy” layer.
With that setup, your bullet GameObjects would ignore all colliders on any other GameObjects that are not on an “enemy” layer, therefore eliminating the need to check for tags.
Of course, if you have other objects that you want your bullets to hit, like terrain for example, you would add another “terrain” layer, assign that layer to all your terrain GameObjects, and edit the collision matrix to allow the “bullet” layer to collide with the “enemy” and “terrain” layers.
Well I’ll be handing it in as is because I’m just going to leave the minions as they are (1 hit kills). For some reason this only happens to these specific minions. I implemented your other code on OTHER enemies and it works perfectly, so you enabled me to make a boss character and a few others. I’m definitely going to look into your suggestion about layers for my next assignment, which starts tomorrow. I kind of get how tags work but it seems like the layers could really keep things neat and organized.
Thanks so much for the help and the great suggestions!