Creating A Progress Bar w/ New Unity Input system

Tyler Smallwood
4 min readMay 4, 2023

--

Objective: Create a system to charge a progress bar.

First thing that needs to be done for today is creating a Slider. In the Hierarchy right click → UI → Slider.

Select the slider and set the position to 0 on the X and Y axis and set the scale to 5. This helps us see it better.

We don’t need the Handle Slide Area, so we can delete that. Next we will set the Fill Area’s Right to 5. This allows the bar to fill up fully.

We need to create a Slider Script and assign it to the slider.

In out PlayerInputactions, we will create a new Action Map named Bar. Next create a Action named FillBar with the F key as the binding.

Now we need to create a script to fill the bar. I named the script Charging, could of probably used a better name but why not for learning, and attached it to the Slider object.

In the Script we will need to get two libraries, the InputSystem and UI. UI is grab the slider information. Next is to get a reference to the PlayerInputActions, Slider, and a bool to see if we are charging.

Now in start we will need to initialize a new PlayerInputActions, Enable the Bar Action Map, and get the slider component. After that we will call the FillBar.performed and canceled actions.

In performed method we will turn _isCharging to true and start a Coroutine. In Canceled we will turn the bool to false.

Since we started a Coroutine, we will need to create a IEnumerator. In the IEnumerator we will create two while loops. One will check to see if we are charging and the other is to check the Slider’s value.

If we are charging we will set the sliders value to increase compared to Time.deltaTime. we will need to use a yield return so we don’t crash unity. I used yield return null but you could use yield return new waitforseconds() to set a time. Null will just wait for the frame to end.

If we stopped charging we will subtract the slider value by Time.deltaTime.

Now we have a working bar to fill.

Now if we want to charge quick and empty slowly, all we have to do is divide Time.deltaTime by a certain amount and it’ll slow by that amount. I’ll do 10 seconds.

That is how we can control a slider with the new input system. Thank you for reading and have a wonderful day!

--

--

Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer