- Home /
Shooting Specific GameObject via Tag
I'm making an MOBA and I have the towers, the map, the health system, the characters, the teams. But I need a script to have the towers shoot missiles/fire at a GameObject. I have my characters in two tags, either the "Red Team" or the "Blue Team". How do I make the tower shoot at a certain GameObject with a certain tag?
What do you mean by "certain"? Based on distance or some other targeting criteria?
Do the towers have a range? You could have a trigger on the tower and when a gameobject enters it, check its tag and start shooting if it's the appropriate one. This would probably be the most simple way, but you could do it heaps of other ways, depending on how you want the tower AI to work. eg:
public string targetTag = "Red Team";
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == targetTag)
Shoot(other.gameObject);
}
@liamcary is right. That's the simplest way to do it.
@darkartsprodigy, you need to be more specific with the behaviour of your tower, so we can figure out which way is better for you.
You can also use distance:
if (Vector3.Distance(target.transform.position, transform.position) <= $$anonymous$$ShootingDistance) {
Shoot();
}