I am creating a feature that when the user clicks on a child of an object the parent scales up a certain ratio and the parent moves so that the child component is at a center point. I have all of that working just fine.
Currently, I am performing the function in a coroutine to lerp between scaling as well as positioning. What I have noticed is that if say I have a large child object that is far away from the focus point, it moves sort of in a curve, because it doesn't have to scale as much but, it has to move a greater distance because of its position. The opposite effect happens when the position to move is small and the scaleTo Vector is larger. Is there a way to counteract the difference in the two Vectors?
So my question is, is there a way to scale and move the object so that its movement isn't so unnatural? I will post my Coroutine below:
IEnumerator LerpScaling(Vector3 scale, Vector3 scaleTo, GameObject obj, bool isReverse)
{
Vector3 offset = new Vector3(0, 0, 2.5f);
Transform baseObj = obj.transform.root.GetComponentInChildren<BaseObject>().transform;
elapsedTime = 0;
while (elapsedTime < timeToLerp)
{
baseObj.localScale = Vector3.Lerp(scale, scaleTo, elapsedTime / timeToLerp);
if (isReverse)
{
//lerp the position back
baseObj.position = Vector3.Lerp(baseObj.position, offset, elapsedTime / Instance.timeToLerp);
}
else
{
//lerp the position
baseObj.position = Vector3.Lerp(baseObj.position, baseObj.position - obj.transform.position + offset, elapsedTime / timeToLerp);
}
elapsedTime += Time.deltaTime;
yield return null;
}
My problem as Draco pointed out was I used baseObj.position
inside of the Lerp.
Instead I stored the position in a Vector3 position
variable right before the while loop so my position lerp looks something like below. I also added a SmoothStep to the time variable to make the Lerp feel more natural.
baseObj.position = Vector3.Lerp(position, offset, Mathf.SmoothStep(0,1, elapsedTime / timeToLerp));