Look At Motion
Looking back at all the available motions I wanted to present a basic one, transform base motion. I also promised I would present the new Builders which are Serialised Objects that can be used in the editor to build a tween. I also went back and modified all the other motion articles to include the updated code of how motions are written now.
Look At Motion is one of my favourite motions that takes advantage of the Transform.LookAt method and it's widely used in the FlowEnt Demo. The reason I like it, it's because it can be used to bring life to a character, by making the character "watch" something. Anyhow, here comes the code!
public class LookAtTransformMotion : AbstractTweenMotion<Transform>
{
[Serializable]
public class Builder : AbstractBuilder
{
[SerializeField]
private Transform target;
public override ITweenMotion Build()
=> new LookAtTransformMotion(item, target);
}
public LookAtTransformMotion(Transform item, Transform target) : base(item)
{
this.target = target;
}
private readonly Transform target;
public override void OnUpdate(float t)
{
item.LookAt(target, Vector3.up);
}
}
public class LookAtVector3Motion : AbstractTweenMotion<Transform>
{
[Serializable]
public class Builder : AbstractBuilder
{
[SerializeField]
private Vector3 target;
public override ITweenMotion Build()
=> new LookAtVector3Motion(item, target);
}
public LookAtVector3Motion(Transform item, Vector3 target) : base(item)
{
this.target = target;
}
private readonly Vector3 target;
public override void OnUpdate(float t)
{
item.LookAt(target, Vector3.up);
}
}
And here is how it's used (snatched this from one of the demo animations!)
new Tween(2.5f).SetEasing(Easing.EaseInOutCubic).For(Character).MoveTo(SplinePoints[0]).LookAt(Vector3.zero)
Last, take a gander at a gif where this was used in the demo
Comments
Post a Comment