프로그래밍/유니티

커브 러프 공식

JinFluenza 2018. 12. 11. 21:08

using System;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;



public class HomingObjectUI : MonoBehaviour

{


   // public bool worldSpace = true;

    public UICurveSet curveSet;

    private float _playTime = 0f;


    private Transform _transform;

    private float _tick = 0f;


    private Vector3 _from;

    private Vector3 _to;


    private bool isPlay = false;

    private Action onFinished;


    public void Initialize(Vector3 startTr, Vector3 endTr, float duration, System.Action endCallBack)

    {

        _transform = this.transform;


        this.gameObject.SetActive(true);


        this.transform.position = startTr;


        _from = startTr;


        _to = endTr;


        curveSet.duration = duration;


        onFinished = endCallBack;


        Ready();


        isPlay = true;

    }


    public void Ready()

    {

       

        _playTime = 0;

        if (this.curveSet.curve_default != null)

            _tick = 1f / (this.curveSet.duration / Time.fixedDeltaTime);

    }


    private void FixedUpdate()

    {

        if (isPlay)

        {

            if (_playTime < 1)

            {

                _playTime += _tick;

                if (_playTime >= 1)

                    _playTime = 1;


                float x = (_from.x * (1f - curveSet.curve_default.Evaluate(_playTime))) + (_to.x * curveSet.curve_default.Evaluate(_playTime)) + curveSet.curve_x.Evaluate(_playTime);

                float y = (_from.y * (1f - curveSet.curve_default.Evaluate(_playTime))) + (_to.y * curveSet.curve_default.Evaluate(_playTime)) + curveSet.curve_y.Evaluate(_playTime);

                float z = (_from.z * (1f - curveSet.curve_default.Evaluate(_playTime))) + (_to.z * curveSet.curve_default.Evaluate(_playTime)) + curveSet.curve_z.Evaluate(_playTime);


                //if (worldSpace)

                 _transform.position = new Vector3(x, y, z);


                //_transform.position = Vector3.Lerp(_from,_to,_playTime);

                //else

                //    _transform.localPosition = new Vector3(x, y, z);

            }

            else

            {

                isPlay = false;

                if (this.onFinished != null)

                    this.onFinished();

            }

        }

    }


}