The main goal is to move an object to target with ease in out speed. The speed should increase at the start to max than near the target to slow down smoothly and stop at the target.
At the top of the script, I did
public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
public Transform start;
public Transform end;
public float duration = 10.0f;
private float t;
At the Start
void Start()
{
t = 0.0f;
}
The Update
void Update()
{
switch (state)
{
case TransitionState.MovingTowards:
var v = destinationTransform.position - transform.position;
if (v.magnitude < 0.001f)
{
state = TransitionState.Transferring;
originTransform = destinationTransform;
timer = 0;
return;
}
t += Time.deltaTime;
float s = t / duration;
transform.position = Vector3.Lerp(transform.position,
destinationTransform.position, curve.Evaluate(s));
break;
case TransitionState.Transferring:
timer += Time.deltaTime;
this.transform.position = Vector3.Lerp(originTransform.position, destinationTransform.position, timer);
if (timer >= 1.0f)
{
this.transform.parent = destinationTransform;
isChild = true;
state = TransitionState.None;
this.enabled = false;
return;
}
break;
default:
this.enabled = false;
return;
}
}
This is where I use the lerp and the animation curve :
t += Time.deltaTime;
float s = t / duration;
transform.position = Vector3.Lerp(transform.position,
destinationTransform.position, curve.Evaluate(s));
The variable destinationTransform is the target.
No matter what value I put in the duration variable the transform is moving too fast to the target. Tried to change the duration to 1 to 100 to 10 same speed.
This is how the animation curve graph is in the inspector :
I changed the animationcurved to be like that in the editor in the inspector but it's not affecting the speed of the transform movement in any way. There is no ease in/out effect. The speed is constant at a very very high speed.
The full script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveToTarget : MonoBehaviour
{
public enum TransitionState
{
None,
MovingTowards,
Transferring
}
public Transform destinationTransform;
public float speed;
public float lerpTime;
public bool isChild = false;
public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
public Transform start;
public Transform end;
public float duration = 10.0f;
private float t;
private Transform originTransform;
private float timer;
private TransitionState state = TransitionState.MovingTowards;
void Start()
{
t = 0.0f;
}
void Update()
{
switch (state)
{
case TransitionState.MovingTowards:
var v = destinationTransform.position - transform.position;
if (v.magnitude < 0.001f)
{
state = TransitionState.Transferring;
originTransform = destinationTransform;
timer = 0;
return;
}
Vector3 moveDir = v.normalized;
//transform.position += moveDir * speed * Time.deltaTime;
t += Time.deltaTime;
float s = t / duration;
transform.position = Vector3.Lerp(originTransform.position,
destinationTransform.position, curve.Evaluate(s));
break;
case TransitionState.Transferring:
timer += Time.deltaTime;
this.transform.position = Vector3.Lerp(originTransform.position, destinationTransform.position, timer);
if (timer >= 1.0f)
{
this.transform.parent = destinationTransform;
isChild = true;
state = TransitionState.None;
this.enabled = false;
return;
}
break;
default:
this.enabled = false;
return;
}
}
}
The part I'm using the lerp this time is to move the transform to the target not to make the transform child like in the other question.
From lines 49 to 52 :
t += Time.deltaTime;
float s = t / duration;
transform.position = Vector3.Lerp(originTransform.position,
destinationTransform.position, curve.Evaluate(s));
start
transform in the first argument, and instead you're moving relative to your current position, creating a feedback loop. \$\endgroup\$