A question to all you mathematics out there :)

thats my methode.

protected float CalculateChance()
        {
            float hitChance = 0;
            hitChance = Random.value;

            Debug.LogError(hitChance);
            return hitChance;
        }

then i call it like

if (CalculateChance() > 0.5f)
                    {
                        do something.....
                    }

right now it call it everytime the random value is > 0.5 and increase a value +1 that starts from 0 and ends on 100.

but i want to make a curve so that at beginning the chance is higher to call it and at end it will reduce.

any one a good formula for that?

Create a public field on your class of type ‘AnimationCurve’ and use that to get some chance value based on some input:

This way you have a gui in the editor to draw the curve.

You read out the value with the Evaluate method.

What to use as an input is up to you. You can use a ‘experience level’, or the ‘time’ or whatever. That’s up to you on what you want dictating the curve.

3 Likes

oh never heard about them. let me check, thank you :slight_smile:

Seriously one of the best tools for manipulating numbers in the 0-1 range, I practically do everything with them besides actual animations, lol.

2 Likes

For real!

1 Like

The above method is probably the best, but just for knowledge sake…
Another way would be to apply the dice roll logic. Get two random numbers between 1-6.

ok i had time to test it, the problem with first solution is, a cant make animationcurve public. its inside a methode that should be private. so i think i cant use it. for the second one, i have no clue how it works, any code examples? :slight_smile:

You can define the AnimationCurve in a public variable outside of the private method, and still use it inside the method. In fact the only time variables can be private or public is when they are member variables, AKA variables that are defined outside of a method.

And if you’re worried about exposing the curve as public, you can set it private and annotate it with [SerializeField]. This will make it appear in the editor but inaccessible from external code.

1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class testAnimCurve : MonoBehaviour
{
    public AnimationCurve myAnimCurve;

    void Update()
    {
        // Try a preset curve and set the right endpoint to "Loop" or "Ping-Pong"
        print(myAnimCurve.Evaluate(Time.time * 0.2f)); // scaled so it's not so fast
    }
}