Implementing the evergreen left Shift key & a visual increment of shield’s strength in Unity
3 min readMay 30, 2021
The exercise here demonstrates two key implementations. One, the left shift key will increase the speed of the player as long as the key remains pressed. Second, the shield’s visual sprite will iterate in between three alpha values to project reception of full strength.
- There is an in-built method named “GetKey( )”, that registers input of a given keycode as long as it stays pressed.
- The reverse is doable using another method named “GetKeyUp( )”, that registers the release of a given keycode.
- Using them, a method can be written which takes care of the left shift key control to raise the speed on demand of the player.
- The speed rates are fixed in this case living in private variables.
- Lastly, this method has to be called in the update( ) method so that the system remains alert at every frame to catch the keypress.
- The result is shown on the link here. For your observation, I have made the speed variable serialized where an increment of approx 50% can be seen in the inspector.
- The release of the shift key would bring the speed back to its normal rate.
The second part of this exercise is explained below.
The task at hand is to mimic the fueling effect on the player when it bumps into the shield power-up. Specifically, the range is 1 to 3, 1 shows the first dump in the strength tank, and the 3rd plus later ones show a full-strength tank.
- Declare a variable that keeps count of the times a shield powerup method gets called and assign a 0 value to it.
private int _shieldMethodCallCount = 0;
- Create a method that handles the switching of alpha channel value associated with the visual. This could be done using if-else or switch statement also. But to limit the code repetition, an array is used.
- Also, remember to increase the call count every time a collision occurs between the shield and player. As well as bring it down to 0 to restart from the beginning the next time a shield is collected.
- Pass the reference of current count in the for loop.
public void ShieldPowerUp()
{
_shieldMethodCallCount++; // With each count, we add 1.
ChangeShieldColor();
_shieldShotActive = true;
_shieldVisual.SetActive(true);
}
------------------------------------------------------------------
public void damage()
{
if (_shieldShotActive == true)
{
_shieldMethodCallCount = 0; // collision brings it to 0.
_shieldShotActive = false;
_shieldVisual.SetActive(false);
return;
}
-------------------------------------------------------------------void ChangeShieldColor()
{
float[] alpha = {0, 0.33f, 0.66f, 1.0f }; // I had to put a 0 for the 0 index since the method call starts from 1. The 0 here helps the alpha to begin at 0.33f.
if (_shieldMethodCallCount < 4)
{
for(int i =0; i<= _shieldMethodCallCount; i++)
{
_shieldVisual.GetComponent<SpriteRenderer>().color = new Color(1,1,1,alpha[_shieldMethodCallCount]);
}
}
else
{
_shieldVisual.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 1);
// The else part will allow the shield to retain the full strength until it gets hit and goes back to 0.
}
}
- The final result is shown on the link here.
Thank you very much