Hi everyone, I’ve been lurking here for a while and finally decided to post. I’m a 3d artist who has a small amount of experience with code, and have found Unity to be amazingly easy to use.
I did a forum search, but I couldn’t find anything on this topic, so here goes. I recently started up a proof of concept game which incorporates different damage types from weapons fire. I adapted the “recievedamage” script from the FPS tutorial, but it doesn’t really work very well for what I have planned currently.
Here is the current sequence of events when applying damage:
- The projectile collides with a given target and sends these lines of code:
function OnTriggerEnter(trigger : Collider) {
// Apply damage.
trigger.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
- The collided object then executes it’s “ApplyDamage” function:
function ApplyDamage (damage : float) {
// We already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0)
return;
hitPoints -= damage;
Now, ideally I want to have several different types of damage which affect different types of objects differently. If I can get the code to recognize different types the rest should be easy, but I wanted to solicit advice as to the best way to do this.
Could I do this by defining different damage types in an array and then write something like trigger.SendMessageUpwards("ApplyDamage", damage, darray[kinetic], SendMessageOptions.DontRequireReceiver);
Or would I have to write a separate “ApplyDamage” function for each type? The latter seems to be the most likely option here, but it feels a little messy to me.
I’d greatly appreciate any advice that anyone can give. Thanks for reading!
This is one of those ‘no right way’ questions.
SendMessage() though only takes one function argument so if you need more than one you should send them wrapped in an array.
However, the array of damage types, at least to me, seems a little overcomplicated. You could do a string name of damage type of an enum as second arg.
function ApplyDamage(args : []) {
damage = args[0]
type = args[1]
if type == 'kinetic' { ... }
Then your trigger would be:
SendMessageUpwards("ApplyDamage", [damage, 'kinetic']);
Please take my code as psuedocode as I don’t use JS. Hope that helps! 
I’m tackling some similar theory, I call a function that calls different types of damage. So I have 2 functions dealing with several types of data. One recognises an input then calls another function to dictate the damage. Recently, I’ve posted a thread on incremental damage - damage over time. From what I’ve learnt so far, it depends on how the gameobjects interact with each and where the information is stored.
I posted something on dom_g’s thread as well. To me, it seems the easiest way is to create a single function that takes multiple args, and then determine how the damage is applied from the args.
For example, you could specify the total amount of damage, often that damage is applied, and for how long that damage is applied:
function ApplyDamage(totalDamage : float, totalTick : float, totalTime : float){
if (totalTime == 0)
health -= totalDamage;
else if (totalTick != 0)
// Apply totalDamage/totalTick every totalTime/totalTick here, needs to loop
else
health -= totalDamage*Time.deltaTime; // Apply totalDamage every second, needs to loop
}
If the time over which the damage is applied is zero, then it is all applied immediately. If the time isn’t zero and the number of ticks isn’t zero, it would apply the total damage divided by number of ticks each second (ie you might get 2 damage per second for 5 seconds for a totalDamage of 10). If the time is >0 but ticks is 0, then you just all the damage applied steadily and each frame over the course of one second.
Does that make any sense?
When damage is applied to the player, it’s pretty much always going to be the same thing happening.
Player Hit:
-Health is subtracted
-Sound is played
-Blood spray? or red flash
-Lag in movement
These would be the most common things that would happen every time.
So if the player is shot in the face. That’s going to happen. If the player gets covered in burning oil, the same thing would be happening. The difference is, the burning oil might continue to apply damage over the course of the duration.
So where as a bullet would just call ApplyDamage(x) on the player. An oil burn would need to attach to the player and do damage for a duration, still using the ApplyDamage to interact with the player object.
In this case I would create a script that does an invokerepeating to send message of applydamage for X amount of damage, maybe plays some burning sounds… and then an invoke that destroys itself in however many seconds/minutes. The missle that does this damage type would just do an addcomponent to the player.
You don’t actually need to use send message though, since you can just do a getcomponent of type player and actually call the function. SendMessage works ok, if you just have one parameter. I wouldn’t get into sending a parametrized array. It gets sloppy. It’s better to just do overloaded methods. The only real overload I would see is one that disables the default blood splash and default sound. That would allow whatever is performing the damage to perform it’s own instead.
Hey everyone, thanks for your responses and help.
I was playing around a bit with the method that Timmer suggested, but in the end I couldn’t get the arrays to work properly without the code getting fairly messy.
I didn’t know how to remotely activate functions, so I thought the sendmessage was the only way to do it. I think I have it working, but I haven’t done extensive testing. Anyways, here are the code snippets, hopefully someone will find them useful at some point.
For Kinetic Projectiles:
// Behavior when there is a collision in the projectile's trigger radius.
function OnTriggerEnter(trigger : Collider) {
// Apply damage.
if (trigger.GetComponent(DamageReciever))
{
trigger.GetComponent(DamageReciever).ApplyDamage(damAmount, damType, damDuration);
}
}
For Electromagnetic (Beam) Weapons:
if (Physics.Raycast (transform.position, direction, hit, range))
{
if (hit.collider.GetComponent(DamageReciever))
{
hit.collider.GetComponent(DamageReciever).ApplyDamage(damAmount, damType, damDuration);
}
}
The “DamageReciever” script:
function ApplyDamage(damAmount : int, damType : String, damDuration : int)
{
// If we already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0)
{
return;
}
// Kinetic Damage
if (damType == "kinetic")
{
// Apply the kinetic damage.
hitPoints -= (damAmount * kineticResistance);
Debug.Log ("F" + Time.frameCount + ": " + gameObject.name + " recieved " + (damAmount * kineticResistance) + " kinetic damage.");
if (hitPoints <= 0.0)
{
Kill();
}
}
// Electromagnetic Damage
if (damType == "electromagnetic")
{
// Apply the EM damage.
hitPoints -= (damAmount * emResistance);
Debug.Log ("F" + Time.frameCount + ": " + gameObject.name + " recieved " + (damAmount * emResistance) + " electromagnetic damage.");
if (hitPoints <= 0.0)
{
Kill();
}
}
}