- Home /
Question by
eugenekim2727 · Nov 12, 2020 at 04:32 PM ·
collision detectionslideroncollisionenterboxcolliderprogress-bar
How to make a progress bar go up after two objects collide?
So I have a progress bar but I would like to know how to make it fill up after my player finishes a task. The idea would be for the player to put objects into other objects (example food into a dog bowl). Here is my entire progress bar script which I got after following along DevDuck's tutorial on progress bars.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Events;
public class ProgressBar : MonoBehaviour { private Slider slider;
public float FillSpeed = 0.5f;
private float targetProgress = 0;
private void Awake()
{
slider = gameObject.GetComponent<Slider>();
}
// Start is called before the first frame update
void Start()
{
IncrementProgress(.00f);
}
// Update is called once per frame
void Update()
{
if (slider.value < targetProgress)
slider.value += FillSpeed * Time.deltaTime;
}
public void IncrementProgress(float newProgress)
{
targetProgress = slider.value + newProgress;
}
void OnCollisionEnter(Collision col){
if(col.gameObject.tag == "dog Bowl"){
IncrementProgress(.5f);}
if(col.gameObject.tag == "dishwasher"){
IncrementProgress(.25f);}
if(col.gameObject.tag == "dishwasher"){
IncrementProgress(.25f);}
}
}
The part at the end was my attempt at getting Unity to recognize a collision and fill up the progress bar after detecting the collision. THANKS!
Comment