Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by sneenlantern · Apr 14, 2012 at 04:53 PM · collidersdamagefirehealth barhealth

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

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · 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
avatar image sneenlantern · Apr 14, 2012 at 05:27 PM 0
Share

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. :)

avatar image
0

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?

Comment
Add comment · Show 5 · 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
avatar image sneenlantern · Apr 14, 2012 at 05:37 PM 0
Share

thank you, I will put this together and get back to you. Thanks for taking the time to explain it.

avatar image fafase · Apr 14, 2012 at 05:42 PM 0
Share

The unity documentation is sometimes a little scarce but you should also have a look at it though.

avatar image sneenlantern · Apr 14, 2012 at 06:52 PM 0
Share

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.

avatar image fafase · Apr 14, 2012 at 07:29 PM 0
Share

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

}

}

avatar image sneenlantern · Apr 14, 2012 at 08:11 PM 0
Share

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

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges