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.
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.
Oh my freaking god, thanks man! Bloody hell, simple changes like and its fixed X'D
Your answer
![](https://koobas.hobune.stream/wayback/20220612123908im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Healthbar system not working correctly 0 Answers
How do i make a colision with a specific object 2 Answers
Asset Store Tools In the drop-down menu no tabs " paskage manager" 0 Answers
Unity Multiplayer: when i hit another player all players health bars go down 0 Answers
gameobject doesn't spawn in the assigned random positions? 2 Answers