playMaker

Author Topic: float lerp  (Read 6800 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
float lerp
« on: January 09, 2013, 03:27:09 PM »
Hi,

 you can lerp vector2 and vector3, but lerping floats is very useful too.


Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2012. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Math)]
[Tooltip("Linearly interpolates between 2 floats.")]
public class FloatLerp : FsmStateAction
{
[RequiredField]
[Tooltip("First Float")]
public FsmFloat fromFloat;

[RequiredField]
[Tooltip("Second Float.")]
public FsmFloat toFloat;

[RequiredField]
[Tooltip("Interpolate between FromFloat and ToFloat by this amount. Value is clamped to 0-1 range. 0 = FromFloat; 1 = ToFloat; 0.5 = half way in between.")]
public FsmFloat amount;

[RequiredField]
[UIHint(UIHint.Variable)]
[Tooltip("Store the result in this float variable.")]
public FsmFloat storeResult;

[Tooltip("Repeat every frame. Useful if any of the values are changing.")]
public bool everyFrame;

public override void Reset()
{
fromFloat = new FsmFloat { UseVariable = true };
toFloat = new FsmFloat { UseVariable = true };
storeResult = null;
everyFrame = true;
}

public override void OnEnter()
{
DoFloatLerp();

if (!everyFrame)
{
Finish();
}
}

public override void OnUpdate()
{
DoFloatLerp();
}

void DoFloatLerp()
{
storeResult.Value = Mathf.Lerp(fromFloat.Value, toFloat.Value, amount.Value);
}
}
}


bye,

 Jean

derkoi

  • Full Member
  • ***
  • Posts: 187
Re: float lerp
« Reply #1 on: January 10, 2013, 01:26:22 PM »
What's the difference between this and ease float?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: float lerp
« Reply #2 on: January 10, 2013, 02:16:49 PM »
Hi,

 very good question:

 ease float is time based. you ease a float in 2 seconds, with lerping, it's a constant catch up of the value with a define "speed" to get there.

 so if your float is a moving target, you can't ease because you are limited in time, you must lerp to continously try to get closer to the target smoothly.

Does that make sense? that's exactly why on that other post, I recommand not using tweening engines to follow moving targets of ayn kind, because the "easing" is a finite somehow, else it doesn't make sense, how can you have a "sin in and out" when it nevers ends...? that's where lerping comes in, because you can let it run, and your target can be moving or not. the "easing" you get it simply defined by the amoung of lerp you inject, the smaller the more time it will take to catch up and the smoother it will be, close to 1, it's almost instant as if it was attached to the target.

Bye,

 Jean

derkoi

  • Full Member
  • ***
  • Posts: 187
Re: float lerp
« Reply #3 on: January 10, 2013, 02:23:30 PM »
I see, sounds very useful. Thanks Jean  ;D