Setting up the Scene
- Create an UI image.
- Set its Pivot.x to 0. This will allow the image to grow out of its side.
- Set its position to wherever you need it.
Creating your C# Script
There is a way to use the new input system close to the old way. We used to use Input.GetKeyDown(Keycode.Space), but now we can use Keyboard.current.spacekey.isPressed. They both mean the same!
For that to work though, you need to add the UnityEngine.InputSystem; namespace.
You will also need to add the UI namespace to grab the RectTransform component from the UI image above.
Grab the reference to the RectTransform in the UI Image and make a float variable to hold the bar’s scale.
I set its local x scale to equal the _barScale variable. So, when I press the space key, the scale changes by 175 units per second. But when I let go, I lose 200 units per second.
I also set the bounds of the bar from 1–825 so it doesn’t fly off the screen.
This means we can just make a new project, slap a script on an object, and get your idea out there just as fast as before!
Next up, I will upgrade a script using the old input system to the new one!
See you then!