Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Animate : MonoBehaviour {
- //MY BLOG: berkinakkaya.wixsite.com/blog
- AnimationCurve curve;
- [Header("Visual Settings")]
- [SerializeField] float heightMultiplier = 2f;
- [SerializeField] float acceleration = 5f;
- [SerializeField] float maxSpeed = 20f;
- [SerializeField] float curveLength;
- [SerializeField] bool startWithDelay;
- [Header("Colors")]
- [SerializeField] Color cold;
- [SerializeField] Color hot;
- float speed;
- SpriteRenderer sprite;
- void Start()
- {
- sprite = GetComponent<SpriteRenderer>();
- CreateCurve();
- }
- public void CreateCurve()
- {
- curve = new AnimationCurve(); //Construct the curve
- //Set Wrap Modes
- curve.preWrapMode = WrapMode.Loop;
- curve.postWrapMode = WrapMode.Loop;
- //Clear the curve
- for(int i = 0; i < curve.length; i++)
- {
- curve.RemoveKey(i);
- }
- Keyframe[] keys = new Keyframe[3];
- if(startWithDelay)
- {
- keys[0] = new Keyframe(0.5f , 0);
- keys[1] = new Keyframe(0.5f+curveLength/2, 1);
- keys[2] = new Keyframe(0.5f+curveLength , 0);
- }
- else
- {
- keys[0] = new Keyframe(0 , 0);
- keys[1] = new Keyframe(curveLength/2, 1);
- keys[2] = new Keyframe(curveLength , 0);
- }
- curve.keys = keys;
- }
- void Update()
- {
- CalculateSpeed();
- float value = curve.Evaluate(Value());
- Scale(value);
- UpdateColor(value);
- }
- void UpdateColor(float value)
- {
- Color color = Color.Lerp(cold, hot, value);
- sprite.color = color;
- }
- void Scale(float height)
- {
- transform.localScale = Vector3.one + Vector3.up * height * heightMultiplier;
- }
- void CalculateSpeed()
- {
- if(speed < maxSpeed)
- speed += (acceleration * Time.deltaTime);
- speed = Mathf.Clamp(speed, 0, maxSpeed);
- }
- float Value()
- {
- return (Time.time * speed) % curveLength;
- }
- }
Add Comment
Please, Sign In to add comment