ArcMovement

mail@pastecode.io avatar
unknown
csharp
2 years ago
943 B
3
Indexable
Never
using System.Collections;
using UnityEngine;

    public class ArcMovement : MonoBehaviour
    {
        public AnimationCurve curve;

        [SerializeField]
        private float duration = 1.0f;
        
        [SerializeField]
        private  float heightY = 3.0f;

        public IEnumerator Curve(Vector3 start, Vector2 target)
        {
            float timePassed = 0f;

            Vector2 end = target;
            //temp vars
            while (timePassed < duration)
            {
                timePassed += Time.deltaTime;

                float linearT = timePassed / duration;//0 to 1 time
                float heightT = curve.Evaluate(linearT);//value from curve

                float height = Mathf.Lerp(0f, heightY, heightT);

                transform.position = Vector2.Lerp(start, end, linearT) + new Vector2(0f, height);//adding values on y axis

                yield return null;
            }
        }
    }