How can I do a collision check with a specific Object?

Hi, I’m really new to Unity scripting and am having trouble getting a certain collision script working.

My player object is a tank with 2 Forms of Health, Hull and Plating. It can shoot either a shell or bullets. Another gameobject I have is a Stationary Turret that fires a bullet.

On the tank; How Can I check that it is the Turret Bullet that has collided with it?

On the turret; How can I check not only whether it is the Shell or the Bullet that it collides with? I need to check which of the two, as they subtract differing amounts of the turret’s health.

If it helps at all, here’s what I have so far…

//in the script attached to the tank

void OnTriggerEnter (Collider other)
	{
		if (other.name == "Bullet")
		{
		 if (OuterPlating > 0)
			{
				
			OuterPlating -= 20;	
			Destroy(other);	
				
			}
	    if (OuterPlating <= 0)
			{			
			HullIntegrity -= 20;	
		    Destroy(other);
			}
		
		}
		
		
	}

and…

//in the script for the turret

void OnTriggerEnter(Collider other)
	{
		
		if (other.name == "TankShell")	
		{
			TurretHealth -= 50;
			Destroy(other);
		}
		else if (other.name == "TankBullet")
		{
			TurretHealth -= 5;	
			Destroy(other);
		}
	}

Hello, what you did it’s right.
For the OnTriggerEnter to work, one of the objects that are colliding with the other needs a rigid body. And probably your tank and your turret already have one. And your bullet and shell need to have the collider marked as trigger.

If at every shot you instantiate a new bullet, what isn’t working is that you are checking for the name of the object, like “TankShell”, “TankBullet” and “Bullet”. When you instantiate a new object he will receive the prefab name plus (Clone) at the end. What you can do its to check fot the tag of the game object. Create tags with those names, select the respective tag to those objects and check for this:

other.tag == "Bullet"

and so on. Take care!

maybe ur colliders arent triggers… u might need to change ur scripts. instead of OnTriggerEnter to OnCollisionEnter(Collision other), or check the trigger checkbox.
ur logic should work

This kind of thing can quickly get out of hand if your code gets big. Imagine a game in which there are 5 different turrets, and the player(or tank) has 20 different weapons. Each of the turrets and the player will need to check for each of the different types of bullets/shells/grenades/RPG rockets/etc. Rather than checking tags, I’d suggest you create a script called Bullet, and apply it to the projectile prefabs. In that script, you can define how much damage that specific projectile does, thus eliminating the if/else blocks, like this:


class Bullet
{
   public int damage;
}

You’d also have a better way to check if the object is actually a bullet of not, by checking if it has a Bullet script attached, like this:


void OnTriggerEnter(Collider other)
{
    Bullet b;
    if ((b = other.transform.GetComponent<Bullet>()) != null)
        health /* or TurretHealth or Shield or whatever */ -= b.damage;
}