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;
}