- Home /
Colliders, Triggers, Damage, and Fire
Hi,
I've found some stuff about this question around here and in other places, but nowhere is it explained to a complete idiot like myself. I am creating a 2D platform game, and I understand the basics of colliders, but not how they activate events other than colliding. I am making a simple platform game and I have a sprite of fire contained in a box collider. I have my character and fire colliding, but I'm not sure about the basics of activating damage to my character. I set up a float value for health in another script, but I don't understand the code for the collision causing damage, and I don't know if other scripts are aware of that value. I've seen some code on the web regarding this situation but I don't understand what it does or where exactly it should go. I've also got Playmaker but I'm not sure if it and my custom health and health bar script are interacting properly as I tried to run the events through Playmaker but nothing is happening. I've got the health bar up on the top of the screen but I haven't been able to instigate any damage to my character yet to see if it works. I would prefer to use straight code but I don't understand quite where to begin. If anybody has the time and inclination, I could use some help. Sorry for being so wordy.
Thanks
Answer by Piflik · Apr 14, 2012 at 05:17 PM
I can show you what I use for adding damage to my characters, maybe it helps you. It is part of the script that controls the character, not part of the projectile's (or, in your case, fire) script.
function OnTriggerEnter(item) {
if(item.tag == "Projectile") {
if(item.GetComponent(Projectile).owner != "enemy") {
health -= item.GetComponent(Projectile).damage;
if(health <= 0){
Instantiate(deathFX, transform.position + Vector3(0, 5, 0), Quaternion.identity);
Destroy(gameObject);
}
}
}
}
I set the colliders to be triggers, so they don't really collide. Your fire would probably have a different tag, or be identified differently.
the only difficulty in changing it to a trigger is I have the flame as an actual sprite and I don't want the player to pass through it, I want him to bump off it in flames but I suppose I can still do that with a trigger and a transform. I suppose the item.tag would be "Fire"... what it know that I mean to refer to the object "fire"? Is that how it works? where would I say how much damage this causes, as I don't want it to be instant death, and how would I say that?
And "Instantiate", huh? That's a beautiful word. :)
Answer by fafase · Apr 14, 2012 at 05:35 PM
First you have various way to check collision:
OnCollisionEnter(other:Collision){}
OnCollisionStay(other:Collision){}
OnCollisionExit(other:Collision){}
The difference is in when do you want to check, at first, while the guy stands "touching" or when the collision ends.
If you tick IsTrigger on your collider
OnTriggerEnter(other:Collider){}
OnTriggerStay(other:Collider){}
OnTriggerExit(other:Collider){}
In this case, the physic engine does not operate and there is no bouncing. Usefult when you want your guy to enter a zone and not get stuck bouncing on it.
Now how to use it:
OnCollisionExit(other:Collision){
// You can do something whenever something collides
if(other.gameObject.tag == "anObject"){
// Do something when anObject collides
}}
Note that other.gameObject contains information on the colliding object.
Now if your guy has a CharacterController
OnControllerColliderHit(hit:ControllerColliderHit){}
Now Example:
This is attached to the player:
var health: int = 10;
function OnCollisionEnter (other: Collision){
if(other.gameObject.tag= "BadGuy"){
health -= 1;
}
}
Got it?
thank you, I will put this together and get back to you. Thanks for taking the time to explain it.
The unity documentation is sometimes a little scarce but you should also have a look at it though.
Ah, I must be totally dumb. Forgive me, I am new to this. That doesn't mean I don't love it though....
This is the script I have for my character's health and the healthbar, and this is up top on my character's script:
public float maxHealth = 75.0f; // $$anonymous$$inimum health is 0.0f (dead)
public float currentHealth = 75.0f; // Players current health
public Texture2D background = null;
public Texture2D energybar = null;
void OnGUI()
{
// Draw the background
GUI.DrawTexture(new Rect(32.0f, 32.0f, 128.0f, 16.0f), background);
// Draw the health/energy bar
GUI.BeginGroup(new Rect(34.0f, 34.0f, 124.0f * (currentHealth / maxHealth), 12.0f));
GUI.DrawTexture(new Rect(0.0f, 0.0f, 124.0f, 12.0f), energybar);
GUI.EndGroup();
}
So I have established what his health is there and that seems to work. Do I put the scripts you described in the same file, or do I attach them to the fire? Now I have two scripts on my character, a controller script and another where I have put my own stuff including animations and this health and GUI. Should I put your code on the controller script, and where?
Sorry, you're going to have to spell it out for me, I haven't done any coding since I was a teenager and I'm in the crash course. But I really want to learn, so your time here is REALLY appreciated. Thanks. In the meantime I will look up this stuff on the Unity manual and try to learn some of this language.
Well, you should attach to the guy what happen to him. When 2 objects interact you need to see what is going on and how to make it easier.
Considering that you can make your health a static variable (see thread on use of static var on this forum) as you only have one and it is then accessible with ScriptName.health.
This way you can have your collision attached to the actual object your player is colliding and subtract to your player.
on the Player.js
static var health;
on the object script
function OnCollisionEnter(other:Collision){
if(other.gameObject.tag == "Player"){
Player.health -= 10; // Static var accessed
Destroy(gameObject);//That destroys the object the script is attached to
}
}
thanks, I'm working on this and I changed health to a static variable and dumped that other script. For the rest of the script, I was wondering if I should change "Player" to "Smith", the name of the game object of my character and the name of my character script. Like this:
function OnCollisionEnter(other:Collision){
if(other.gameObject.tag == "Smith"){
Smith.health -= 25; // Static var accessed
Also, I've been using c#. Will this work accordingly the same since you're using javascript? Oh, I changed the subtraction of his health to 25 because I made his health 100 and wanted it taken off in quarters.
Your answer
Follow this Question
Related Questions
Health Bar fire damage 1 Answer
How to create/fix fire damage script???? 1 Answer
Fire Damage 2.0 1 Answer
How to cause damage on collision? 1 Answer
Health Regeneration 2 Answers