Checking for last hit and changing teams...

I am trying to create a game like Phage Wars and in that game when you attack a building all the way to zero you have captured the building and it is in your team.

How will I add a way to find who attacked the building last before it was at zero? That way I can change the building's team accordingly.

Also, should I change teams by changing material (color of team) or should I destroy old gameObject and instantiate a new one?

Please post some code along with answer. (Script preferably Javascript)

Thanks! :D

Not really a coder but I can point you in the right direction to investigate I think.

If there are only 2 teams like Red versus Blue then it is easy as all you need to do is award the "Blue building" to the "Red" team upon destruction (or vice-versa). Therefore no detection beyond the usual "Oh I'm dead" is needed to start some other function or coroutine.

If there are multiple teams (i.e. Red, Blue, Green, etc) it becomes a little more tricky and somewhat dependent upon your games mechanics and design but should still be very doable using basic collision detection.

CASE 1 - projectile detection

All you really need to determine is who fired the last shot. One way is to give each teams projectiles a tag like "Blue Bullet", "Green Bullet" etc. Then when the target, which has a rigidbody and a script attached that handles OnCollisionEnter, is defeated/destroyed/captured it checks the tag of the GameObject that collided with it using something like (psuedocode)

function OnCollisionEnter(other : Collision) 
var whatHitMe: String = other.gameObject.tag;
    switch whatHitMe: {
        case "Green Bullet":
            //Do something here
        case "Red Bullet";
            //do something here

Then in the function ApplyDamageCausedByGreen if the whatHitMe damage caused death/capture/etc award the kill to the appropriate team.

You could also use collision.contacts to determine "other" in the collision.


If you aren't basing it on a projectile rigidbody collision you could use OnTriggerEnter/Stay instead, like here

Depending on the game design one could also use a proximity detection scheme whereby you simply check if an opposing teams player or players are within range of the object when it is captured/destroyed, with multiple teams you could then check which team had more players in range and award the win to the team with the most players. Kind of a king of the hill capture point thing.

But if the game relies on detecting exactly who fired the last shot then onCollisionEnter or even OnTriggerEnter should do the trick. Check out the Collision action matrix on this page to ensure that a collision event will be fired.

The FPS example might has enough collision detection code that can then be hacked apart to not just check for the 'Player" tag but insted detect the projectiles name or tag.

Hope this at least gets you pointed in the right direction. Maybe a more experienced coder can pipe in with a real world example.