Search Unity

A question to all you mathematics out there :)

Discussion in 'Scripting' started by hublard, Jan 14, 2021.

  1. hublard

    hublard

    Joined:
    Aug 19, 2015
    Posts:
    79
    thats my methode.

    Code (CSharp):
    1. protected float CalculateChance()
    2.         {
    3.             float hitChance = 0;
    4.             hitChance = Random.value;
    5.  
    6.             Debug.LogError(hitChance);
    7.             return hitChance;
    8.         }
    then i call it like

    Code (CSharp):
    1. if (CalculateChance() > 0.5f)
    2.                     {
    3.                         do something.....
    4.                     }
    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?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    Create a public field on your class of type 'AnimationCurve' and use that to get some chance value based on some input:
    https://docs.unity3d.com/ScriptReference/AnimationCurve.html

    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.
     
    seejayjames, SparrowGS and Vryken like this.
  3. hublard

    hublard

    Joined:
    Aug 19, 2015
    Posts:
    79
    oh never heard about them. let me check, thank you :)
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Seriously one of the best tools for manipulating numbers in the 0-1 range, I practically do everything with them besides actual animations, lol.
     
    seejayjames and lordofduct like this.
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    For real!
     
    SparrowGS likes this.
  6. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    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.


     
  7. hublard

    hublard

    Joined:
    Aug 19, 2015
    Posts:
    79
    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? :)
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    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.
     
  9. JoNax97

    JoNax97

    Joined:
    Feb 4, 2016
    Posts:
    611
    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.
     
    PraetorBlue likes this.
  10. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class testAnimCurve : MonoBehaviour
    6. {
    7.     public AnimationCurve myAnimCurve;
    8.  
    9.     void Update()
    10.     {
    11.         // Try a preset curve and set the right endpoint to "Loop" or "Ping-Pong"
    12.         print(myAnimCurve.Evaluate(Time.time * 0.2f)); // scaled so it's not so fast
    13.     }
    14. }
    15.