Search Unity

Move Value From A to B via Animation Curves

Discussion in 'Scripting' started by keenanwoodall, May 22, 2015.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    598
    I'm trying to make a simple coroutine that moves the position of an object to a specified Vector. I want it to do it along an animation curve so that I can over shoot the target and move back to it. However, whenever my curve goes over or up to one, it arrives at the destination and doesn't go anywhere else.
    upload_2015-5-22_12-41-53.png
    Here's my current code.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. public static class CurveMovement
    8. {
    9.     public static IEnumerator MoveTo(Transform transform, Vector3 goal, AnimationCurve curve, float time)
    10.     {
    11.         float timer = 0f;
    12.         while(timer <= time)
    13.         {
    14.             transform.position = transform.position + ((goal - transform.position) * curve.Evaluate(timer / time));
    15.             timer += Time.deltaTime;
    16.             yield return new WaitForEndOfFrame();
    17.         }
    18.         yield break;
    19.     }
    20. }
    21.  
    Also, from what I can tell, when the speed can never go lower that the max y-value that the animation curve has returned. I made the curve plateau and then drop back down, but the speed didn't slow down at all.
     
    Last edited: May 22, 2015