Move To Motion

A while ago I introduced Echoes, a new, revolutionary, animation concept brought to the market by FlowEnt, and I talked a bit about certain motions, but I feel like I should go into more detail with the MoveTo motion for Echo, because it's one of my favourite ones.

I remember in the day when I was playing World of Warcraft and was doubling the account so I can level up a Priest with my main, and I was using the "/follow" command and thus controlling the second character. I always loved this utility, and when I created Echoes it was the first thing I wanted to do. And this is where the MoveTo motion came in. It has two flavours: MoveToVector and MoveToTransform. Since the vector one is a bit dull(vector value doesn't change) I'll talk about the transform one where we can move the transform.

Here is a small Echo that I created using the new Builder where I used 5 motions: 3 for input, 1 MoveTo and, 1 LookAt.

And this is how it works:

Pretty neat, isn't it? Anyway. In order to support an open-source community, here is the code!

public class MoveToTransformMotion : MoveToVectorMotion
{
    [Serializable]
    public new class Builder : AbstractFloatSpeedTypeBuilder
    {
        [SerializeField]
        private Transform target;

        public override IEchoMotion Build()
            => new MoveToTransformMotion(item, target, speed, speedType);
    }

    public MoveToTransformMotion(Transform item, Transform target, float speed = DefaultSpeed, SpeedType speedType = DefaultSpeedType) : base(item, target.position, speed, speedType)
    {
        this.target = target;
    }

    protected new readonly Transform target;

    public override void OnUpdate(float deltaTime)
    {
        base.target = target.position;
        base.OnUpdate(deltaTime);
    }
}
public class MoveToVectorMotion : AbstractFloatSpeedTypeMotion<Transform>
{
    [Serializable]
    public class Builder : AbstractFloatSpeedTypeBuilder
    {
        [SerializeField]
        private Vector3 target;

        public override IEchoMotion Build()
            => new MoveToVectorMotion(item, target, speed, speedType);
    }

    public MoveToVectorMotion(Transform item, Vector3 target, float speed = DefaultSpeed, SpeedType speedType = DefaultSpeedType) : base(item, speed, speedType)
    {
        this.target = target;
    }

    protected Vector3 target;

    public override void OnUpdate(float deltaTime)
    {
        item.position = Vector3.MoveTowards(item.position, target, GetSpeed(Vector3.Distance(item.position, target)) * deltaTime);
    }
}

Comments

Popular Posts