- Home /
Question by
rayith · May 28 at 11:25 PM ·
sliderregenerationregenerate
sanity regenerate overtime
so I have a sanity system that drops over time and is tied to a slide bar. I want when you enter a triggered collider sanity stops going down and starts slowly regenerating, also stops when full. Any suggestions how I would go about this?
Here is my sanity script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class sanitymeter : MonoBehaviour
{
[Header("Matches Parameters")]
public float MaxSanity = 100f;
public float Sanity = 0f;
public float santiyOT = 0.008f;
public Slider SanitySlider;
void Start()
{
}
void Update()
{
Sanity = Sanity - santiyOT * Time.deltaTime;
SanitySlider.value = Sanity / MaxSanity;
}
}
Comment
Best Answer
Answer by rayith · May 29 at 04:23 PM
solved it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class sanitymeter : MonoBehaviour
{
[Header("Sanity Parameters")]
public float MaxSanity = 100f;
public float Sanity = 0f;
public float santiyOT = 0.008f;
public float RegSanityOT = 0.008f;
public Slider SanitySlider;
public bool notinlight;
public AudioSource heartbeat;
void Start()
{
notinlight = true;
}
void Update()
{
if (notinlight == true && Sanity >= 0)
{
Sanity = Sanity - santiyOT * Time.deltaTime;
SanitySlider.value = Sanity / MaxSanity;
if (Sanity == 75)
{
heartbeat.Play();
}
if (Sanity <= 50)
{
FirstPerson.OnTakeDamage(5 * Time.deltaTime);
}
if (Sanity <=1)
{
FirstPerson.OnTakeDamage(50);
}
}
else if (notinlight == false && Sanity <= 100)
{
Sanity = Sanity + RegSanityOT * Time.deltaTime;
SanitySlider.value = Sanity / MaxSanity;
}
}
void OnTriggerStay(Collider target)
{
if (target.tag == "candle")
{
notinlight = false;
}
}
private void OnTriggerExit(Collider coll)//entrance and exit to/from trigger
{
if (coll.tag == "candle")
{
notinlight = true;
}
}
}
Your answer
Follow this Question
Related Questions
How to send Slider Information over websocket? 1 Answer
Gradient issue with slider 1 Answer
How to load audio slider values from PlayerPrefs at the beginning of the game? 2 Answers
Slider Puzzle but with Multiple Tiles per Block 0 Answers
How to make slider function properly after an animation? 2 Answers