• 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 ygh-jk · Jun 10, 2017 at 04:19 PM · not workinghealthbar

If detection not working quite as expected.

I've worked on a main script for the player that contains both movement for the iso controls and health and damage. Movements works as expected but when it comes to health, it doesn't work as I'd expected to do. When the health reaches 0 after taking damage, the Alive bool is supposed to turn off and stop player movement.

What actually happens is that when the player gets hits one more time after 0 and the CurHealth is below 0 and not at 0 is when the player actually stops.

The code below:


using UnityEngine; using System.Collections;

public class PlayerMain : MonoBehaviour {

 //Variables
 
 Vector3 forward, right;//m
 float moveSpeed = 4f;//m
 
 private Vector3 moveDirection;//m
 private bool moveEnabler;//m
 
 public float gravity = 3F;//m
 public float MaxHealth = 100f;//h
 public float CurHealth;//h
 public GameObject Health;//h
 public bool alive;//h
 
 
 void Start()
 {
     
 //M/ove script    
 forward = Camera.main.transform.forward;//camera dependence
 forward.y = 0;//keeps character facing forward
 forward = Vector3.Normalize(forward);
 right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
 
 //H/ealth script
 alive = true;
 CurHealth = MaxHealth;
 SetHealth ();

 }
 
 public void TakeDamage(float amount)
 {
     /*if (!alive)
     {
         return;
         Move();
     }//checks if health is 0, if so code below return does not run within the curly brackets
     */
     
     if(CurHealth <= -0f)
     {
         alive = false;
         //gameObject.SetActive(false);
     }
     
     CurHealth -= amount;
     SetHealth ();
 }
 

  void FixedUpdate() 
  {
 
      CharacterController controller = GetComponent<CharacterController>();// is the controller on the ground?
     
     if(( (Input.GetAxis("Horizontal") != 0) || Input.GetAxis("Vertical") != 0) && alive == true)//disables allowing any keys aside from WASD and UP DOWN LEFT RIGHT keys
         {
         moveEnabler = true;
         }else 
         {
         moveEnabler = false;
         }
     
     if(controller.isGrounded && moveEnabler == true)
     Move();

 
     moveDirection.y -= gravity * Time.deltaTime; //Applying gravity to the controller
     
     controller.Move(moveDirection * Time.deltaTime); //Making the character move

     
  }
  
  void Move()
  {
      Vector3 direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
     Vector3 rightMovement = right * moveSpeed * Time.deltaTime * Input.GetAxis("Horizontal");
     Vector3 upMovement = forward * moveSpeed * Time.deltaTime *Input.GetAxis("Vertical");

     Vector3 heading = Vector3.Normalize(rightMovement  + upMovement);//controls where its facing
     
     transform.forward = heading;
     transform.position += rightMovement;
     transform.position += upMovement;
  }
  

 public void SetHealth()
 {
     float P_Health = CurHealth / MaxHealth; //if CurHealth 80/100 = 8f
     //P_Health value vaule between 0 and 1
     Health.transform.localScale = new Vector3(Mathf.Clamp (P_Health, 0f, 1f), Health.transform.localScale.y, Health.transform.localScale.z);
 }

}


Been trying to adjust the code but I cant find what might be a simple fix so please any help would be appreciated.

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

Answer by Aaranos · Jun 10, 2017 at 09:24 PM

You check if the character's health is below or equal to 0 before taking damage. Try doing the check after you subtract the damage.

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 ygh-jk · Jun 11, 2017 at 02:19 PM 0
Share

Oh my freaking god, thanks man! Bloody hell, simple changes like and its fixed X'D

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

108 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 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 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 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 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

Healthbar system not working correctly 0 Answers

Getting Obsolete Error after Downloading Standard Assets Package 1 Answer

Enemy kill counter only counts first killed enemy 0 Answers

How do I make my healthbar change color depending on damage? 1 Answer

Need enemy to attack player and deplete health 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