UnityGameProjectsCode / Rise2Point0Game / Player / PlayerColorTransition.cs
PlayerColorTransition.cs
Raw
using UnityEngine;

public class PlayerColorTransition : MonoBehaviour
{
    private ApplyForceToPlayer applyForce;
    private ApplyForceController applyForceController;
    private ApplyForceTouch applyForceTouch;
    private SpriteRenderer playerSprite;

    private float startTime;
    private Color startingColor;
    private Color endingColor;
    private Color playerColor; //color the player is when out of a fade
    public float transitionSpeed;

    private bool startTransition;

    public Color PlayerColor { get => playerColor; set => playerColor = value; }
    public Color EndingColor { get => endingColor; set => endingColor = value; }

    private void Start()
    {
        applyForce = ObjectManager.GetObject(3).GetComponent<ApplyForceToPlayer>();
        applyForceController = ObjectManager.GetObject(3).GetComponent<ApplyForceController>();
        applyForceTouch = ObjectManager.GetObject(3).GetComponent<ApplyForceTouch>();
        playerSprite = ObjectManager.GetObject(0).GetComponent<SpriteRenderer>();

        startingColor = Color.red;
        endingColor = Color.green;
        playerColor = Color.green;
    }

    private void Update()
    {
        if (applyForce.ForceApplied || applyForceTouch.ForceApplied || applyForceController.ForceApplied)
            OnForceApplied();

        if (startTransition)
        {
            float distCovered = (Time.time - startTime) * transitionSpeed;
            float fracJourney = distCovered / 1;
            playerSprite.color = Color.Lerp(startingColor, endingColor, fracJourney);


            if (playerSprite.color == endingColor)
            {
                playerSprite.color = startingColor;
                startTime = Time.time;
                startTransition = false;
                playerSprite.color = playerColor;
            }
        }
    }

    public void OnForceApplied()
    {
        startTransition = true;
        startTime = Time.time;
    }
}