RandomProbability.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks
  3. {
  4. [TaskDescription("The random probability task will return success when the random probability is above the succeed probability. It will otherwise return failure.")]
  5. public class RandomProbability : Conditional
  6. {
  7. [Tooltip("The chance that the task will return success")]
  8. public SharedFloat successProbability = 0.5f;
  9. [Tooltip("Seed the random number generator to make things easier to debug")]
  10. public SharedInt seed;
  11. [Tooltip("Do we want to use the seed?")]
  12. public SharedBool useSeed;
  13. public override void OnAwake()
  14. {
  15. // If specified, use the seed provided.
  16. if (useSeed.Value) {
  17. Random.InitState(seed.Value);
  18. }
  19. }
  20. public override TaskStatus OnUpdate()
  21. {
  22. // Return success if random value is less than the success probability. Otherwise return failure.
  23. float randomValue = Random.value;
  24. if (randomValue < successProbability.Value) {
  25. return TaskStatus.Success;
  26. }
  27. return TaskStatus.Failure;
  28. }
  29. public override void OnReset()
  30. {
  31. // Reset the public properties back to their original values
  32. successProbability = 0.5f;
  33. seed = 0;
  34. useSeed = false;
  35. }
  36. }
  37. }