Hey so i need help.
I want to make so the player would giggle after 5 seconds when their sanity is less than 30.
I have multiple sounds that i can use and i want them to play randomly.
Here’s the script:
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class sanity : MonoBehaviour
{
public RawImage vignetteUI;
private float transparency;
private float spinerotation;
private float armsrotation;
private float InsaneGiggles;
private float twitch;
public int sanitynumber;
public AudioSource GiggleSource;
public AudioClip[] SanityGiggles;
public TextMeshProUGUI sanityindicator;
public PlayerController mymovement;
public sanity Sanity;
public Texture normalface;
public Texture lowsanotyface;
public Material headmaterial;
[SerializeField] private Transform spine;
[SerializeField] private Transform leftarm;
[SerializeField] private Transform rightarm;
[SerializeField] private Transform head;
void Start()
{
sanitynumber = 100;
StartCoroutine("facetwitch");
}
void Update()
{
sanityreturn();
spinereturn();
armsrotationreturn();
InsaneGigglesreturn();
sanitynumber = Mathf.Clamp(sanitynumber, 0, 100);
sanityindicator.text = sanitynumber + "%";
vignetteUI.color = new Color(0f, 0f, 0f, transparency);
}
void LateUpdate()
{
if (sanitynumber <= 90 && mymovement.enabled)
{
spine.transform.localRotation = Quaternion.Euler(spinerotation, 0f, 0f);
leftarm.transform.localRotation = Quaternion.Euler(0f, 0f, armsrotation);
rightarm.transform.localRotation = Quaternion.Euler(0f, 0f, armsrotation / -1f);
}
if (sanitynumber <= 40)
{
head.transform.localRotation = Quaternion.Euler(0f, 0f, twitch);
}
if (sanitynumber < 100)
{
headmaterial.SetTexture("_MainTex", lowsanotyface);
}
else
{
headmaterial.SetTexture("_MainTex", normalface);
}
}
private float sanityreturn()
{
if (sanitynumber <= 100)
{
transparency = 0f;
}
if (sanitynumber <= 80)
{
transparency = 0.2901961f;
}
if (sanitynumber <= 60)
{
transparency = 0.4117647f;
}
if (sanitynumber <= 40)
{
transparency = 0.6686275f;
}
if (sanitynumber <= 20)
{
transparency = 0.8686275f;
}
if (sanitynumber <= 10)
{
transparency = 1f;
}
return transparency;
}
private float spinereturn()
{
if (sanitynumber <= 100)
{
spinerotation = 0f;
}
if (sanitynumber <= 80)
{
spinerotation = 1f;
}
if (sanitynumber <= 60)
{
spinerotation = 2f;
}
if (sanitynumber <= 40)
{
spinerotation = 4f;
}
if (sanitynumber <= 20)
{
spinerotation = 6f;
}
if (sanitynumber <= 10)
{
spinerotation = 8f;
}
return spinerotation;
}
private float armsrotationreturn()
{
if (sanitynumber <= 90)
{
armsrotation = 72f;
}
if (sanitynumber <= 80)
{
armsrotation = 70f;
}
if (sanitynumber <= 60)
{
armsrotation = 68f;
}
if (sanitynumber <= 40)
{
armsrotation = 65f;
}
if (sanitynumber <= 20)
{
armsrotation = 60f;
}
if (sanitynumber <= 10)
{
armsrotation = 60f;
}
return armsrotation;
}
private IEnumerator facetwitch()
{
twitch = Random.Range(44.03f, -44.03f);
yield return new WaitForSeconds(0.05f);
twitch = 0f;
yield return new WaitForSeconds(0.2f);
twitch = 0f;
StartCoroutine("facetwitch");
}
}