Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

set random trigger?

Discussion in '2D' started by RuneShiStorm, Feb 23, 2018.

  1. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Hi all!
    I have 2 "Damage-animations" for my enemy and I want the enemy to trigger One of Two animations randomly every time I hit him. (like 50/50 change he trigger Damage1 and Damage2)
    My current script looks like this:

    Code (csharp):
    1.  
    2. if (!IsDead)
    3.         {
    4.             MyAnimator.SetTrigger("damage");
    5.             if (healthStat.CurrentValue > 10)
    6.             {
    7.                 MyAnimator.SetTrigger("damage");
    8.             }
    9.  
    and I tried a simple "or"---

    Code (csharp):
    1.  
    2.             {
    3.                 MyAnimator.SetTrigger("damage") || MyAnimator.SetTrigger("damage2");
    4.             }
    5.  
    Didn't work... I tried to Google "How to set random trigger" without any results.
    I'm not sure what Im looking for here :S Is this a "random" behavior or a "string" thingi?
    Thanks in advance!
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    try Random.Range:
    Code (CSharp):
    1. int rnd = Random.Range (0, 2);
    2. if (rnd == 0)
    3. //trigger 1
    4. else
    5. //trigger 2
    or

    Code (CSharp):
    1. float rnd = Random.Range (0, 1);
    2. if (rnd > 0.5f)
    3. //trigger 1
    4. else
    5. //trigger 2
     
  3. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    thanks man ! i will give it a shot!