- Home /
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
Up to you really, I'd change the colour ins$$anonymous$$d of instantiating though.
I did something similar. What I did was basically had a Red $$anonymous$$m and Blue $$anonymous$$m with corresponding Tags. When the object being attacked was destroyed I called a coroutine which was passed the Tag of whoever fired the final shot. So if a player from Blue Tag $$anonymous$$m fired the final shot that destroyed the object his $$anonymous$$m won it.
That game is almost identical to the one I am making :P! How would you do the coroutine to pass the tag part? That is what I am missing... Please Help! Thanks!
Answer by Julian-Glenn · Jul 18, 2010 at 06:58 PM
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":
ApplyDamageCausedByGreen();
//Do something here
break;
case "Red Bullet";
ApplyDamageCausedByRed();
//do something here
break;
}
}
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.
CASE 2
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.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
BoxCast, OverlapArea, and Raycasting; ground detection questions 0 Answers
2D pong game. HELP 1 Answer
Make Player Move Slow Over Grass 1 Answer
How to do a raycast to make my player unable to move through certain objects? 2 Answers