Critical DMG script

The Script I have bellow works for damage and health but the critical chance part isn’t working. I inserted an image as well to show that its showing up properly in the unity Inspector as well

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AttributesManager : MonoBehaviour
{
    public int health;
    public int attack;
    public float critDamage = 1.5f;
    public float critChance = 0.5f;

    public void TakeDamage(int amount)
    {
        health -= amount;
        Vector3 randomness = new Vector3(Random.Range(0f, 0.25f), Random.Range(0f, 0.25f), Random.Range(0f, 0.25f));
        DamagePopUpGenerator.current.CreatePopUp(transform.position, amount.ToString(), Color.red);

        if (health <= 0)
            Destroy(gameObject); // Destroy the current object

    }

    public void DealDamage(GameObject target)
    {
        var atm = target.GetComponent<AttributesManager>();
        if (atm != null)
        {
            float totalDamage = attack;

            //Count RNG Chance
            if(Random.Range(0f, 1f) < critChance)
            {
                totalDamage *= critDamage;
            }

            atm.TakeDamage((int)totalDamage);

        }

     
    }




}

9845805--1417035--critchance.png

Debug.Log what Random.Range is returning. Make sure you assign it’s value to a variable and then debug that value.
Next, if it is triggering the if statement, make sure you double check the value of totalDamage. This is usually the first step in debugging code! Just checking all your values.

Thank you! I’m really new at this and not sure how to Debug.Log

When in doubt the documentation should be your first port of call.

The docs on Debug.Log give examples on how to use it: https://docs.unity3d.com/ScriptReference/Debug.Log.html

Namely, use it to print out relevant information to the console so you can see where the data of your logic is going wrong.

For example:

float critRoll = Random.Range(0f, 1f);
Debug.Log("Critical Roll is: " + critRoll, this);

if(critRoll < critChance)
{
    Debug.Log("Critical hit!");
    totalDamage *= critDamage;
}
1 Like

awesome thank you, but where do i put that code? at the bottom of the code i posted?

It’s just an example modification to your existing code starting from line 32 of your code posted above. You’d modify your code to match.