using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FloatingZ : MonoBehaviour {

    public AnimationCurve Curve;
    public AnimationCurve SizeCurve;
    public GameObject Player;

    private float Clock = 0;
    private float y = 0;
    public float TimeToLive = 6f;

    void Start() {
        y = transform.position.y;
    }

    void Update() {
        var position = transform.position;
        Clock += Time.deltaTime;
        TimeToLive -= Time.deltaTime;
        if (TimeToLive <= 0) {
            Destroy(gameObject);
        }
        if (Clock > 1) {
            Clock = 0;
            y += 0.5f;
        }
        position.y = y + Curve.Evaluate(Clock) * 0.5f;
        position.x -= 0.4f * Time.deltaTime;
        position.z += 0.4f * Time.deltaTime;
        transform.position = position;

        var rot = transform.rotation;
        rot.SetLookRotation(Player.transform.position - position);
        transform.rotation = rot;

        var scale = transform.localScale;
        var size = SizeCurve.Evaluate((6 - TimeToLive) / 6);
        scale.Set(size, size, size);
        transform.localScale = scale;
    }
}