Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by importguru88 · Apr 14, 2016 at 12:10 AM · playerdamageenemyaiapplyhealth

Applying damage to the player health problem

The player max health is at 100f . But I only want to take -5f . The problem I am having its taking to much health . When the enemy attacks the player health goes from 100f to -1045f very fast. I don't know why. For example the player health should be 95f. Here is my attack script :

using UnityEngine; using System.Collections;

public class attack1 : MonoBehaviour { public GameObject player; public GameObject bear; public float distance; private health2016 health2016Script; public static bool isPlayerAlive = true;

 // Use this for initialization
 void Start () {
 

 
 
 }
 
 // Update is called once per frame
 void Update () {
 player = GameObject.FindWithTag("Player");
 bear = GameObject.Find("bear");
 if (isPlayerAlive)
 {  
 
 }

 distance = Vector3.Distance(player.transform.position, bear.transform.position);
 
 if (distance <= 3)
 {    
     GameObject Player = GameObject.FindWithTag("Player");
     health2016Script = Player.GetComponent<health2016>();
     health2016Script.max_health -= 5f;
     GetComponent<Animation>().CrossFadeQueued("move" , 0.3f, QueueMode.PlayNow);
     GetComponent<Animation>().CrossFade("attack");
 }
 
 else if (distance < 15 && distance > 3.0f) {
     GetComponent<Animation>().CrossFadeQueued("attack", 0.3f, QueueMode.PlayNow);
     GetComponent<Animation>().CrossFade("move");
     transform.LookAt(player.transform.position);

     
 
 
 
 }
 
 }
 

}

and Player health script :

using UnityEngine; using System.Collections;

public class health2016 : MonoBehaviour {

public float max_health = 100f;

 void ApplyDamage()
 {
     if (max_health <= 0f)
     {
         Destroy(gameObject);
     }
     
 }

}

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
1

Answer by KurtRussellsMullet · Apr 14, 2016 at 02:39 AM

Hi There!

I believe what is happening is that your attack is happening every frame once the characters are within a certain distance of one another.

The below code that is reducing your characters health is in the Update() function which is called every single frame (Reference: Unity Video on Update Function ):

 if (distance <= 3)
  {    
      GameObject Player = GameObject.FindWithTag("Player");
      health2016Script = Player.GetComponent<health2016>();
      health2016Script.max_health -= 5f;
      GetComponent<Animation>().CrossFadeQueued("move" , 0.3f, QueueMode.PlayNow);
      GetComponent<Animation>().CrossFade("attack");
  }

Additionally, this unity tutorial video on player health covers a good example better than I could. 7 minutes into the video is where he begins the discussion on how the damage system hits the health. He uses a variable called "damaged" within a TakeDamage function. Damaged is set to true during the TakeDamage function call, but damaged is also updated to "False" every single frame, so that the player does not take continuous damage every frame.

Hope this helps!

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[Solved] I can't call Method from Referenced Script 1 Answer

Why is my player not getting more damaged? 0 Answers

Can't add script behaviour VisualContainerAsset",Can't Add Script "The script needs to derive from ,Im getting the Cant add script behavior VisualContainerAsset. the script needs to drive from MonoBehaviour 0 Answers

Apply Damage Universally 0 Answers

Enemy wont deal damage 0 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