Search Unity

Player not taking Health Damage

Discussion in 'Editor & General Support' started by kentg1, Jan 14, 2019.

  1. kentg1

    kentg1

    Joined:
    Dec 22, 2014
    Posts:
    168
    I have a player health game ( player is yellow) I am starting - a cube trying to pass three obstacles

    I have fooled around with the rigid body on the player and also freezing the rotation when it hits a object

    at times the players hits the last two obstacles - or at times has gone right through them, The last obstacles either goes right through the player or hits it and does not do damage at all?

    Thank you


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class plankRotate : MonoBehaviour {
    4.     public float Speed = 50f;
    5.     public float Damage = 10f;
    6.  
    7.     void Start () { }
    8.  
    9.     void Update ()
    10.     {
    11.         transform.Rotate(Vector3.up * Time.deltaTime * Speed);
    12.     }
    13.  
    14.     void OnCollisionEnter (Collision other)
    15.     {
    16.         var health = other.gameObject.GetComponent<HealthScript>();
    17.         if (health != null)
    18.             health.TakeDamage(Damage);
    19.     }
    20. }

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class HealthScript : MonoBehaviour {
    4.     public GameManager Manager;
    5.     public GameObject HealthBarCanvas;
    6.     public playermove Movement;
    7.     public MeshRenderer MeshRenderer;
    8.     public RectTransform BarTransform;
    9.     public float max_health = 600f;
    10.     public float cur_health;
    11.     public bool alive = true;
    12.  
    13.     float minDelta = .01f;    //SizeDelta "Off" where bar is empty
    14.     float maxDelta = 1.8f; //SizeDelta "On" where bar is full
    15.  
    16.     void Start () {
    17.         Reset();
    18.     }
    19.  
    20.     public void TakeDamage(float amount) {
    21.         if (!alive) return;
    22.  
    23.         cur_health -= amount;
    24.         SetHealthBar();
    25.  
    26.         if (cur_health  <= 0)
    27.             Die();
    28.     }
    29.  
    30.     public void SetHealthBar () {
    31.         float barRatio = minDelta + (maxDelta - minDelta) * Mathf.Clamp(cur_health / max_health, 0f, 1f);
    32.         BarTransform.sizeDelta = new Vector2(barRatio, BarTransform.sizeDelta.y);
    33.     }
    34.  
    35.     public void Reset() {
    36.         cur_health = max_health;
    37.         alive = true;
    38.  
    39.         HealthBarCanvas.SetActive(true);
    40.         SetHealthBar();
    41.  
    42.         Movement.enabled = true;
    43.         MeshRenderer.enabled = true;
    44.     }
    45.  
    46.     public void Die() {
    47.         cur_health = 0;
    48.         alive = false;
    49.  
    50.         //Stop the player from moving.
    51.         Movement.enabled = false;
    52.  
    53.         //Make the player disappear
    54.         MeshRenderer.enabled = false;
    55.  
    56.         //Disable the health bar
    57.         HealthBarCanvas.SetActive(false);
    58.  
    59.         //Tell the GameManager we died
    60.         Manager.OnDeath();
    61.     }
    62. }
    63.  
     

    Attached Files: