- Home /
Setting up smart damage taking in game...
I'm trying to set up a game like Phage Wars.
In that game when your troops collide they either support (if it's your building) or attack (if it's the enemies building) the buildings.
I want to know how to set up a way to tell whether to add or subtract to the number of people when a soldier collides a building. How would I do that?
(This depends on whether you control or they control the building and which team the soldier is on).
Answer by Peter G · Jul 14, 2010 at 12:41 AM
I would have either tags or variables for each group, then on collision, check to see if the soldiers tag matches the building's tag and if it does then send a message to the building to add defenses and hide the soldier. Otherwise, send the soldier into attack mode.
A similar and easy way would be to make an enum for the different groups, give every object a script which decides that enum i.e. red team, blue team... then use GetComponent to grab that script, compare the enum of the soldier and the building and check to see if they match. This would be helpful if you were using tags for something else already.
ex.
//This script goes on the player. function OnCollisionEnter(col : Collision) { if(col.gameObject.CompareTag(this.tag)) { col.gameObject.BroadCastMessage("AddReinforcments", SendMessageOptions.DontRequireReceiver); HideSoldier(); }
else {
Attack(col.gameObject);
}
}
function HideSoldier () { renderer.enabled = false; }
function Attack (target : GameObject) { //attack the gameObject; } //Didn't test it. Check for typos.
See http://msdn.microsoft.com/en-us/library/sbbt4032(VS.80).aspx for info on enumerations. They look very similar in JS. enum Team{Blue, Red}; I think. I can't quite remember how to do them in JS. They create like a multiple choice where the value can be one of the selected. You shouldn't need to change this.tag. You have to remember that "this" is a monobehaviour which is a type of component. Components have a tag variable so this.tag is saying thisComponent.tag .
Edit. I think the pickup item script in the lerpz tutorial shows how to use enums in js. the enum is something like enum Pickup{health, fuelcell}; Edit 2: In my oppinion, $$anonymous$$SND gives way more information than they have to and it can be very intimidating. I would also recommend searching the term on the internet and getting a simpler idea because you don't need a very in-depth knowledge for what you are doing.
I got an error.... what does this mean? "Actor::update$$anonymous$$assFromShapes: Compute mesh inertia tensor failed for one of the actor's mesh shapes! Please change mesh geometry or supply a tensor manually!"
Answer by Callum McIntyre · Jul 13, 2010 at 11:38 PM
You would use a standard collision script to detect collisions - if you haven't got that in place yet, I suggest you search it.
You'd then have an if statement inside the function called on any soldier-building collision.
You'd check if the building collider is owned by you or the enemy - you'll have to set a variable each time a building is captured.
If it's yours the soldier will run a function to add to the variable, if it's the enemies you'd run a function to subtract from it.
The difficult bit is the collision detection, and that's not something I'm good with. I'd suggest you search it, or run through the tutorials which explain it very well.
Well I already made this list myself in my head. The question was more how would I do it in script.... Thanks for the effort though. If you put it in code you will get a Checkmark and an Upvote but for now only and UpVote. If you add code I will give Checkmark :P! Again, Thanks for effort.
Your answer
Follow this Question
Related Questions
My rockets don't do damage? 1 Answer
Bootcamp damage 0 Answers
AI Damage if touched 1 Answer
FPS Tutorial AI Damage not Working 1 Answer
The enemy don´t lose health, but why??? 0 Answers