top of page

CharacterPowerUp.cs

using UnityEngine;
using UnityEngine.VFX;

#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif

public class CharacterPowerUp : MonoBehaviour
{
    [Header("Component References")]
    public Animator animator;
    public VisualEffect levelUpVFX;

    [Header("Hotkey Settings (Legacy Input)")]
    [Tooltip("Press this key to trigger the power up effect if using Legacy Input.")]
    public KeyCode powerUpHotkey = KeyCode.Space;

    private bool isPoweringUp = false;

    void Update()
    {
        bool inputTriggered = false;

#if ENABLE_INPUT_SYSTEM
        // Unity 6 New Input System fallback check for Space key
        if (Keyboard.current != null && Keyboard.current.spaceKey.wasPressedThisFrame)
        {
            inputTriggered = true;
        }
#else
        // Legacy Input Manager check
        if (Input.GetKeyDown(powerUpHotkey) || Input.GetButtonDown("Fire1"))
        {
            inputTriggered = true;
        }
#endif

        if (inputTriggered && !isPoweringUp)
        {
            StartPowerUp();
        }
    }

    public void StartPowerUp()
    {
        isPoweringUp = true;

        // Trigger character power up animation (which includes material glow keyframes)
        if (animator != null)
        {
            animator.SetTrigger("PowerUp");
        }

        // Play particle system inside VFX Graph
        if (levelUpVFX != null)
        {
            levelUpVFX.Reinit(); // Ensures graph resets state cleanly in Unity 6
            levelUpVFX.Play();
        }
    }

    // Call via Animation Event at the end of the power up clip to reset state
    public void OnPowerUpComplete()
    {
        isPoweringUp = false;
    }
}

bottom of page