Code Yoshi's Skyward Soar

...
For this project, I used the new input system. As you can see I went behavior-wise with invoke unity events. I used this for walking and jumping, and I used the ability.
The walk and jump input is used in a very general PlayerMovement script. Below, I will go deeper into the ability input and ability scripts.
                                
//This function gets called for the input
public void OnAbility(InputAction.CallbackContext context)
{
    pressingAbility = context.ReadValue<float>();
}

private void Update()
{
//If you press the input (space bar) and you're not on cooldown
    if (pressingAbility == 1 && !onCooldown)
    {
        onCooldown = true;
        StartCoroutine(OnCooldown());
        StartCoroutine(InAbility());
    }
    //If you're in the ability
    if (inAbility)
    {
        FindObjectOfType<UpgradeSystem>().GetUpgradeStates();
        playerWings.SetActive(true);
        onCooldownTime = 0;
    }
    //If you're not in the ability
    else
    {
        onCooldownTime += Time.deltaTime;
        playerWings.SetActive(false);
        FindObjectOfType<UpgradeSystem>()
        .shieldObject.SetActive(false);
        FindObjectOfType<UpgradeSystem>()
        .shield.shieldHealth = 0;
        slider.value = onCooldownTime;
    }
}
                                
                            
The top function gets all the input from the player, in this case, if the player presses the space bar. And it reads the value as a float, so that means: 0=false, not true, and 1=true. So if you press the space bar it will be 1 and when you're not pressing it, it's 0.

And if you look in the update in the if statement if the input is 1, so when you press the space bar, go into ability and on cooldown. That's necessary because otherwise you could just spam the space bar and reactivate the ability over and over again.

Lastly, if you are in the ability you will get wings visually and the cooldown timer starts. The function that gets called I will go into detail below.
                                
//If enough wings collected, upgrade
public void GettingUpgrade()
{
    if (upgradePart >= upgradePartsNeeded && upgradeState < 3)
    {
        upgradeState += 1;
        upgradePart = 0;
        upgradePartsNeeded = upgradePartsNeeded + 2;
    }
}

//All upgrades
public void GetUpgradeStates()
{
    if (upgradeState == 1)
    {
        // 20% more move speed while walking & sprinting
        playerMovement.moveSpeed = playerMovement.boostedWalkMoveSpeed;
        playerMovement.moveSpeed = playerMovement.boostedSprintMoveSpeed;
    }

    if (upgradeState == 2)
    {
        // 20% more move speed while walking & sprinting + 20% jump boost
        playerMovement.moveSpeed = playerMovement.boostedWalkMoveSpeed;
        playerMovement.moveSpeed = playerMovement.boostedSprintMoveSpeed;
        playerMovement.jumpForce = playerMovement.boostedJumpForce;
    }
    if (upgradeState == 3)
    {
        /* 2 Extra Health + 
        20% more move speed while walking & sprinting + 20% jump boost */
        shield.shieldHealth = 2;
        shieldObject.SetActive(true);
        playerMovement.moveSpeed = playerMovement.boostedWalkMoveSpeed;
        playerMovement.jumpForce = playerMovement.boostedJumpForce;
    }
}
                                
                            
The function at the top is keeping track if you have enough wing parts collected. And if you do, you will go to the next upgrade state. But the new upgrade state has more parts you need to collect to go to the one after that stage. So it gets more difficult to level up with each level.

The function below is for what the upgrade is, there are 3 stages. The higher you go in stage number, the stronger you get. This makes more sense because you will also need more parts to collect, so it's scaled very nicely.
Results:

The Full Scripts
|
v


                            
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;

public class AngelForm : MonoBehaviour
{
    //Input
    private PlayerInput playerInput;
    private InputAction abilityInput;

    private float pressingAbility;

    //Cooldown
    public float cooldownTime;
    private bool onCooldown;

    //Ability
    [SerializeField] float abilityTime;
    private bool inAbility;

    [SerializeField] GameObject playerWings;

    [SerializeField] Slider slider;
    [SerializeField] TextMeshProUGUI readyText;

    private float onCooldownTime;

    private void Start()
    {
        playerInput = new PlayerInput();
        abilityInput = playerInput.Player.Walk;
        abilityInput.Enable();
    }

    public void OnAbility(InputAction.CallbackContext context)
    {
        pressingAbility = context.ReadValue<float>();
    }

    private void Update()
    {
        if (pressingAbility == 1 && !onCooldown)
        {
            onCooldown = true;
            StartCoroutine(OnCooldown());
            StartCoroutine(InAbility());
        }
        if (inAbility)
        {
            FindObjectOfType<UpgradeSystem>().GetUpgradeStates();
            playerWings.SetActive(true);
            onCooldownTime = 0;
        }
        else
        {
            onCooldownTime += Time.deltaTime;
            playerWings.SetActive(false);
            FindObjectOfType<UpgradeSystem>().shieldObject.SetActive(false);
            FindObjectOfType<UpgradeSystem>().shield.shieldHealth = 0;
            slider.value = onCooldownTime;
        }

        if (!onCooldown)
        {
            readyText.text = "READY!";
        }
        else
        {
            readyText.text = "";
        }
    }

    private IEnumerator InAbility()
    {
        inAbility = true;
        yield return new WaitForSeconds(abilityTime);
        inAbility = false;
    }

    private IEnumerator OnCooldown()
    {
        yield return new WaitForSeconds(cooldownTime);
        onCooldown = false;
    }
}

                            
                        
                            
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class UpgradeSystem : MonoBehaviour
{
    [SerializeField] float upgradeState;
    [SerializeField] float upgradePartsNeeded;
    public float upgradePart;

    private PlayerMovement playerMovement;
    public Shield shield;

    public GameObject shieldObject;

    //UI
    [SerializeField] Slider slider;
    [SerializeField] TextMeshProUGUI upgradePartsText;
    [SerializeField] TextMeshProUGUI upgradeStateText;

    private void Awake()
    {
        playerMovement = FindObjectOfType<PlayerMovement>();
        shield = FindObjectOfType<Shield>();
        shieldObject.SetActive(false);
    }

    public void GettingUpgrade()
    {
        if (upgradePart >= upgradePartsNeeded && upgradeState < 3)
        {
            upgradeState += 1;
            upgradePart = 0;
            upgradePartsNeeded = upgradePartsNeeded + 2;
        }
    }

    private void Update()
    {
        //UI
        slider.value = upgradePart;
        slider.maxValue = upgradePartsNeeded;
        upgradePartsText.text = upgradePart.ToString() + " / " + upgradePartsNeeded.ToString();
        upgradeStateText.text = "Power: " + upgradeState.ToString();
    }

    public void GetUpgradeStates()
    {
        if (upgradeState == 1)
        {
            // 20% more move speed while walking & sprinting
            playerMovement.moveSpeed = playerMovement.boostedWalkMoveSpeed;
            playerMovement.moveSpeed = playerMovement.boostedSprintMoveSpeed;
        }
    
        if (upgradeState == 2)
        {
            // 20% more move speed while walking & sprinting + 20% jump boost
            playerMovement.moveSpeed = playerMovement.boostedWalkMoveSpeed;
            playerMovement.moveSpeed = playerMovement.boostedSprintMoveSpeed;
            playerMovement.jumpForce = playerMovement.boostedJumpForce;
        }
        if (upgradeState == 3)
        {
            // 2 Extra Health + 20% more move speed while walking & sprinting+ 20% jump boost
            shield.shieldHealth = 2;
            shieldObject.SetActive(true);
            playerMovement.moveSpeed = playerMovement.boostedWalkMoveSpeed;
            playerMovement.jumpForce = playerMovement.boostedJumpForce;
        }
    }
}
                            
                        

Go back

Yoshi

or

Home Page