PlayerAnimation.cs 1.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAnimation : MonoBehaviour
{
	public AnimationCurve m_AirScale;
	public float m_Time = 10f;

	private bool m_isAnimating = false;
	private bool m_isScaled = false;

	private MovementController r_Controller;

	// Start is called before the first frame update
	void Start()
	{
		r_Controller = GetComponent<MovementController>();
	}

	// Update is called once per frame
	void Update()
	{
		ScalePlayer();
	}

	public void ScalePlayer()
	{
		if (Input.GetKeyDown(KeyCode.P) && !m_isAnimating)
		{
			if (!m_isScaled)
			{
				StartCoroutine(ScaleUp(m_Time));
			}
			else
			{
				StartCoroutine(ScaleDown(m_Time));
			}
		}
	}

	private IEnumerator ScaleUp(float t)
	{
		m_isAnimating = true;
		m_isScaled = true;
		Vector3 s = transform.localScale;
		float step = 1 / t;
		for (float i = 0; i <= 1; i += step)
		{
			float x = m_AirScale.Evaluate(i);
			transform.localScale = s + new Vector3(x * Mathf.Sign(s.x), x, x);
			r_Controller.GroundCheckDistance += x/2;
			yield return new WaitForFixedUpdate();
		}

		m_isAnimating = false;
	}

	private IEnumerator ScaleDown(float t)
	{
		m_isAnimating = true;
		m_isScaled = false;
		Vector3 s = transform.localScale;
		float step = 1 / t;
		for (float i = 0; i <= 1; i += step)
		{
			float x = m_AirScale.Evaluate(i);
			transform.localScale = s - new Vector3(x * Mathf.Sign(s.x), x, x);
			r_Controller.GroundCheckDistance -= x/2;
			yield return new WaitForFixedUpdate();
		}

		m_isAnimating = false;
	}
}