• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by MikezNesh · Jul 18, 2010 at 02:00 AM · 2dcollisiongameteams

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

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spinaljack · Jul 18, 2010 at 02:02 AM 2
Share

Up to you really, I'd change the colour instead of instantiating though.

avatar image Julian-Glenn · Jul 18, 2010 at 03:15 AM 2
Share

I did something similar. What I did was basically had a Red team and Blue team 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 team fired the final shot that destroyed the object his team won it.

avatar image MikezNesh · Jul 18, 2010 at 04:40 AM 0
Share

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!

avatar image MikezNesh · Jul 18, 2010 at 04:41 AM 0
Share

@Spinaljack: how do you change material via scripting?

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Make Player Move Slow Over Grass 1 Answer

Easy Way To Enable A Certain BoxCollider2D? 0 Answers

CharacterController "breaks" collision, goes haywire 2 Answers

3d Collision for 2d objects 1 Answer

2D pong game. HELP 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges