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 BigBlob · Mar 13, 2012 at 06:16 PM · colliderdamagesubstract healthhealth

Damage to Health

I have to scripts. One is The player's Health and the other one is Enemy script for the enemy. So basicly the enemy follows the player and collides, then the health has to be subtracted.


Health :

 var h00 : Texture2D;
 var h10 : Texture2D;
 var h20 : Texture2D;
 var h30 : Texture2D;
 var h40 : Texture2D;
 var h50 : Texture2D;
 var h60 : Texture2D;
 var h70 : Texture2D;
 var h80 : Texture2D;
 var h90 : Texture2D;
 var h100 : Texture2D;
 
 static var HEALTH = 100;
 
 function Update()
 {
     var g_Health = gameObject.Find("g_Health");
     
     if(HEALTH > 90)
     {
         g_Health.guiTexture.texture = h100;
         return;
     }
     else if (HEALTH > 80)
     {
         g_Health.guiTexture.texture = h90;
         return;        
     }
     else if (HEALTH > 70)
     {
         g_Health.guiTexture.texture = h80;
         return;        
     }
     else if (HEALTH > 60)
     {
         g_Health.guiTexture.texture = h70;
         return;        
     }
     else if (HEALTH > 50)
     {
         g_Health.guiTexture.texture = h60;
         return;        
     }
     else if (HEALTH > 40)
     {
         g_Health.guiTexture.texture = h50;
         return;        
     }
     else if (HEALTH > 30)
     {
         g_Health.guiTexture.texture = h40;
         return;        
     }
     else if (HEALTH > 20)
     {
         g_Health.guiTexture.texture = h30;
         return;        
     }
     else if (HEALTH > 10)
     {
         g_Health.guiTexture.texture = h20;
         return;        
     }
     else if (HEALTH > 5)
     {
         g_Health.guiTexture.texture = h10;
         return;        
     }
     else if (HEALTH <= 0)
     {
         g_Health.guiTexture.texture = h00;
         Application.LoadLevel(1);
         return;        
     }
 }


Enemy :

 var distance;
     var target : Transform;    
     var lookAtDistance = 15.0;
     var attackRange = 10.0;
     var moveSpeed = 5.0;
     var damping = 6.0;
     
     private var isItAttacking = false;
 
     function Update () 
     {
     distance = Vector3.Distance(target.position, transform.position);
 
     if(distance < lookAtDistance)
     {
     isItAttacking = false;
     renderer.material.color = Color.yellow;
     lookAt ();
     }   
     if(distance > lookAtDistance)
     {
     renderer.material.color = Color.green; 
     }
     if(distance < attackRange)
     {
     attack ();
     }
     if(isItAttacking)
     {
     renderer.material.color = Color.red;
     }
 }
 
 function lookAt ()
 {
 var rotation = Quaternion.LookRotation(target.position - transform.position);
 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
 }
 
 function attack ()
 {
     isItAttacking = true;
     renderer.material.color = Color.red;
     transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
 }

Anyone please help me with this, I want the enemy to subtract health from the player when the enemy collides with him.

  • Felipe

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
2
Best Answer

Answer by fafase · Mar 16, 2012 at 06:52 AM

You HEALTH variable is static. You should read about static variables as starters sometimes think static means accessible outside which is 1/2 true and 1/2 false.

In the enemy script add a function:

 function OnCollisionEnter(hit:Collision){
      if(hit.gameObject.name=="Player"){
      Player.HEALTH -= 10;}  //Here the base is scriptname.variablename
 }



Now let's say you want to use SendMessage: in your player script:

 function DecreaseHealth () {
  HEALTH -= 10;  //You are in the script so you don't need the scriptname first
 }

in your enemy script:

 function OnCollisionEnter(col : Collision) {
 
      if(col.gameObject.name == "Player"){
       gameObject.Find("Player").SendMessage("DecreaseHealth");
      }}

What is does basically is like sending a message with a name on it (DecreaseHealth) and only the appropriate receiver is going to look at it.

Now to finish, as I already advised you, have a look at switch statement to make your script more efficient. if statements run one case after the other and continues if wrong.A switch does not rely on previous cases, the program can re-organize to get the fastest execution.

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 BigBlob · Apr 06, 2012 at 01:05 AM 0
Share

Thanks, oh and it wasn't about the variable type - it's the name. So I changed it to Life ins$$anonymous$$d of HEALTH, BTW the variable had to be changed to private. Thanks a lot!!!

avatar image
0

Answer by Meltdown · Mar 13, 2012 at 06:26 PM

Look at using a combination of OnCollisionEnter and SendMessage to achieve what you need.

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 Meltdown · Mar 13, 2012 at 06:29 PM 0
Share

The second link is Send$$anonymous$$essage http://unity3d.com/support/documentation/ScriptReference/GameObject.Send$$anonymous$$essage.html

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

Help with health reduction script 1 Answer

Animation + Attack when in range 1 Answer

This collision Enemy health code suddenly stopped working. 1 Answer

Two objects with different damage - Not working? 0 Answers

How to implement fall damage 1 Answer


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