- Home /
Better way of Animating linear movement than AnimationCurve.Linear()?
Hi All,
We are developing a 3D RPG game for iOS, but movement is restricted to a tile based view similar to many retro titles such as Dungeon Master.
I've been moving the player using physics, but I've just re-worked the collision system so that we don't need to rely on the collision model and instead we have a pre-baked map of whether a tile can be entered and how one can move from tile to tile, so I am wondering if it is cheaper to move the player by animating them from tile position to tile position, given that I already know these positions and whether the tile is free or not.
So I can do this by creating an AnimationCurve via AnimationCurve.Linear but I presume this is creating a new curve, but I wonder if it's coming from a pool of curves and whether by using this I will (or won't) increase garbage collection. Were I on pro I'd profile it to see, but I am not sadly :-(
I was hoping to create one animation clip, squirrel the curves and keys away and update them but this doesn't seem to work and results in a lot of method calls compared to the code below:
public class BVMap2DController : MonoBehaviour
{
AnimationClip m_anim;
Transform m_trans;
Animation m_animation;
const string m_clip_name_move = "M";
void Start()
{ // get local transform matrix & animation
m_trans = transform;
m_animation = animation;
// create the animation clip for moving in 3 dimensions
m_anim = new AnimationClip();
}
public void AnimateToPosition(ref Vector3 dest, float speed)
{ // play the animation
m_anim.ClearCurves();
Vector3 pos = m_trans.localPosition;
AnimationCurve curve_x = AnimationCurve.Linear(0, pos.x, speed, dest.x);
AnimationCurve curve_y = AnimationCurve.Linear(0, pos.y, speed, dest.y);
AnimationCurve curve_z = AnimationCurve.Linear(0, pos.z, speed, dest.z);
m_anim.SetCurve("", typeof(Transform), "localPosition.x", curve_x);
m_anim.SetCurve("", typeof(Transform), "localPosition.y", curve_y);
m_anim.SetCurve("", typeof(Transform), "localPosition.z", curve_z);
m_animation.AddClip(m_anim, m_clip_name_move);
m_animation.Play(m_clip_name_move);
}
}
I've not dabbled in this area before, so I'm open to more efficient ways. I wasn't sure what title to go with, partly I'd like to know about the garbage collection on the phone, partly the cheapest solution.
Motivation for this is that I wanted to push the heavy lifting into the native code instead of doing this in Update() or within a Coroutine. Naturally removing the physics dependency ought to be quicker.
Thanks H
Currently binned using animations and now using Coroutines with WaitForFixedUpdate().
Answer by Eric5h5 · Sep 25, 2011 at 12:28 AM
I'm not 100% clear as to what your question is, but if you're talking about moving something from one spot to another, just use Lerp. See GridMove.
Well I can do that.
Rightly or wrongly I've got it into my head that it's better to have fewer update methods and things done natively are more efficient, so I'm trying to avoid having an update call for each creature in my game - I could have one manager update of course that updates all creatures currently moving.
I was thinking I could create or modify an animation for each move from one tile to the next and that the animation would run more efficiently than having updating in Update(). However in trying to do this I am using AnimationCurve.Linear() which creates me a new AnimationCurve, I am doing this for each axis and creating an AnimationClip containing these curves. I can then play the clip and animate between two points but how efficient is that - there's a lot of setup; will the garbage collector run a lot more because of the AnimationCurves I am making for each movement? Would updating with Lerp be more efficient?
I guess I could also be asking how expensive Update() and FixedUpdate() are whether Coroutines() are more efficient or, as suggested, I use Lerp, perhaps in one master Update() that updates all currently moving creatures and players?
So I've posted another question (http://answers.unity3d.com/questions/170243/how-efficient-is-update-vs-update-via-a-manager-vs.html) tackling some of the wider issues that have me exploring ways of doing this differently than just using Lerp in Update().
I suppose using Lerp is the answer, but not quite what I was looking for.