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
1
Question by eeveelution8 · Apr 13, 2014 at 07:43 PM · getcomponentstatshealth-deductionhealth

enemy attack not recognizing players stats

I've coded a script for enemy attacks, and they are supposed to lower the players HP by the designated amount, but lower it depending on the players defense stats.

This is the script attached to the attack object:

 var damage = 0;
 var FIREdamage = 0;
 var ICEdamage = 0;
 var SHOCKdamage = 0;
 
 
 
 function OnCollisionEnter (myCollision : Collision) {
  if(myCollision.gameObject.name == "Player"){
   var health : player_script = myCollision.gameObject.GetComponent(player_script);
   health.HP = health.HP - damage * (1 - (health.Physical_resist / 100));
   health.HP = health.HP - FIREdamage * (1 - (health.Fire_resist / 100));
   health.HP = health.HP - ICEdamage * (1 - (health.Ice_resist / 100));
   health.HP = health.HP - SHOCKdamage * (1 - (health.Shock_resist / 100));
    
  }
 }

In the game, I set the damage to 5, and the players Physical_resist stat is at 75, so the player should concievably recieve 75% less damage, but when playing the level, the player stills receives 5 damage.

This is the players script

 var LVL : int = 1;
 
 var EXP : int = 0;
 var Upgradepoints : int = 5;
 
 var STR = 1;
 var MAG = 1;
 var END = 1;
 
 var Fire_resist = 0;
 var Ice_resist = 0;
 var Shock_resist = 0;
 var Physical_resist = 0;
 
 
 var MAXHP : int = 10;
 var HP : int = 10;
 var MAXMP : int = 10;
 var MP : int = 10;
 
 var deathlevel : String;
 
 var infotext : GUIText = gameObject.GetComponent(GUIText);
 
 var MPtext : GUIText = gameObject.GetComponent(GUIText);
 var HPtext : GUIText = gameObject.GetComponent(GUIText);
 var MPMAXtext : GUIText = gameObject.GetComponent(GUIText);
 var HPMAXtext : GUIText = gameObject.GetComponent(GUIText);
 
 var leveltext : GUIText = gameObject.GetComponent(GUIText);
 var exptext : GUIText = gameObject.GetComponent(GUIText);
 
 
 
 
 
 function Start () {
     InvokeRepeating("check",0,0.001);
     
     
     
     
     
 }
 
 function check () {
 
 MAXMP = 10 + (MAG * LVL);
 MAXHP = 10 + (END * LVL);
 
     if(EXP > LVL*10) {
     LVL = LVL + 1;
     EXP = 0;
     Upgradepoints = Upgradepoints + 1;
     }
 
 MPtext.text =  "MP "+MP;
 HPtext.text =  "HP "+HP;
 HPMAXtext.text =  "/  "+MAXHP;
 MPMAXtext.text =  "/  "+MAXMP;
 
 leveltext.text =  "LEVEL:"+LVL;
 exptext.text =  "EXP:"+EXP;
 
 }
 
 
 
 
 function Update () {
     if(HP < 0){
      
     Application.LoadLevel(deathlevel);
     
     }
     if(MP < 0){
      
     MP = 0;
     
     }
     if(MP > MAXMP){
      
     MP = MAXMP;
     
     }
     if(HP > MAXHP){
      
     HP = MAXHP;
     
     }
     
 
 }


What is wrong with the scripts?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by MasterPDON · Apr 13, 2014 at 08:38 PM

75% of 5 is not an int(integer). You'll get a decimal number, which will be approximated to the nearest integer. Try changing the ints to floats.

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 eeveelution8 · Apr 13, 2014 at 09:58 PM 0
Share

works great now, thanks!

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

22 People are following this question.

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

Related Questions

Implementing Damage to Gun, best way to implement health and damage. 1 Answer

irregular decrease of the lives in health control 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Running a script function using "GetComponent" 1 Answer

Using multiple instances of the same script 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