Как написать код что бы по кнопке воспроизводилась анимация с шансом 10% 90% нечего не происходилою
Есть кнопка. Она считает клики по ней. Но мне нужно что бы с каким то шансом при каждом клике проигрывалась анимация выпадения сундука. Вот этот код не работает. он с вероятностью в 10% вырубает кнопку(она перестает нажиматься и считать клики. Возможно я что то в самой юнити начудил. Есть анимация AnimCheats, аниматор AnimCheats. В аниматоре start -> пустая анимация -> AnimCheats через тригер Play.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.EventSystems;
public class MainMenu : MonoBehaviour
{
[SerializeField] int money;
public Text moneyText;
public Animation anim;
public AnimationClip a;
private void Start()
{
money = PlayerPrefs.GetInt("money");
}
public void ButtonClick()
{
money++;
PlayerPrefs.SetInt("money",money);
if (Random.Range(1.0f, 100.0f) > 90)
{ anim = GetComponent<Animation>();
anim.Play("Play");
}
}
public void ToAch()
{
SceneManager.LoadScene(1);
}
void Update()
{
moneyText.text = money.ToString();
}
}
Ответы (1 шт):
Автор решения: Денис Сарумян
→ Ссылка
Решил так
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.EventSystems;
public class MainMenu : MonoBehaviour
{
[SerializeField] int money;
[SerializeField] int chest;
public Text moneyText;
public Text chestText;
public Animator anim;
private void Start()
{
money = PlayerPrefs.GetInt("money");
chest = PlayerPrefs.GetInt("chest");
}
public void ButtonClick()
{
money++;
float x = Random.Range(1f, 101f);
if (x <= 5f)
{
anim.SetTrigger("Play");
chest++;
}
PlayerPrefs.SetInt("money",money);
PlayerPrefs.SetInt("chest",chest);
}
public void ToAch()
{
SceneManager.LoadScene(1);
}
void Update()
{
moneyText.text = money.ToString();
chestText.text = chest.ToString();
}
}