AnimationCurve error and solution

Hi,
I read the documentation about animation curves, here : Unity - Scripting API: AnimationCurve.AnimationCurve
When I tried to run the supplied script, I got the following error :

It is easy to fix, by creating a new Vector3. If you use this code though and apply it on a cube, you will see the cube going up and to the right, disappearing from the screen.
Perhaps using the WrapMode as follows could provide a more interesting example. You get a cube that goes up and down. User can modify the curve characteristics in Editor during runtime and watch how the cube motion changes.

using UnityEngine;
using System.Collections;

public class AnimCurveExample : MonoBehaviour
{
    public AnimationCurve curve ;
   
    void Start()
    {
        curve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(1, 1));
        curve.preWrapMode = WrapMode.PingPong;
        curve.postWrapMode = WrapMode.PingPong;
    }
   
    void Update()
    {
        transform.position = new Vector3(transform.position.x, curve.Evaluate(Time.time), transform.position.z);
    }
}

Thanks Ippokratis! Not only for reporting the problem, but also providing an improved replacement. We really appreciate the effort. This will be in the next online update of the docs, (this friday!)

2 Likes