I have a border to idict which character was selected, and I want to move that when user click on specific character avatar, I think doing a smooth moving of the selecter border will be neat effect. Here is what is looks like:
So I am using following script to move the dashed border
public class SelectCharacter : MonoBehaviour, IInputBehavior {
[SerializeField]
private Character character;
private IEnumerator moveLocalPosOverSeconds(GameObject obj, Vector3 end, float seconds)
{
float elapsedTime = 0;
Vector3 start = obj.transform.localPosition;
while(elapsedTime < seconds)
{
obj.transform.localPosition = Vector3.Lerp(start, end, (elapsedTime / seconds));
elapsedTime += Time.deltaTime;
yield return new WaitForEndOfFrame();
}
// hope this will ajust the dashed box to precise postion
transform.localPosition = end;
}
public void OnClick()
{
GameObject selectBorder = transform.parent.Find("SelectBorder").gameObject;
Vector3 newLocalPos = new Vector3(transform.localPosition.x, selectBorder.transform.localPosition.y, selectBorder.transform.localPosition.z);
Debug.Log("localPos is :" + newLocalPos);
StartCoroutine(moveLocalPosOverSeconds(selectBorder, newLocalPos, 0.2f));
SceneController.instance.ChangeCharacterTo(character);
}
}
And it seems that the position of the border wont end up to the precisely postion as desired, I know that doing Lerp
with time may case some precise position so add transform.localPosition = end;
to last in hoping for ajust the postion after the while
block. but it won't work.
And I double check the log of the console and the postion flied of the dashed box in ther inspector, it just won't end up in the precise position.
some time this will happen:
And if I got luck this seems to be good:
But in the inspector:
The x value should be precisely 0.9 instead of 0.88...
What did I do wrong?