2

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:

enter image description here

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:

enter image description here

And if I got luck this seems to be good:

enter image description here

But in the inspector:

enter image description here

The x value should be precisely 0.9 instead of 0.88...

What did I do wrong?

armnotstrong
  • 8,605
  • 16
  • 65
  • 130
  • 3
    Incrementing elapsedTime BEFORE changing the position should help. – FLX Mar 31 '17 at 10:48
  • @FLX I am wondering why the last line of the coroutine won't tweak the position to its precisely desire – armnotstrong Mar 31 '17 at 10:54
  • 1
    Every frame you move your sprite for the distance corresponding to the deltaTime of the PREVIOUS frame. The very last frame is completely ignored. There is nothing to wonder you get different, but always incorrect, results. – rs232 Mar 31 '17 at 10:58
  • 1
    @rs232 why the last line of `transform.localPosition = end; ` won't do the tweak? – armnotstrong Mar 31 '17 at 11:50
  • @armnotstrong I see, I think you should replace this last line with transform.localPosition = new Vector3(end.x, end.y, end.z); I'm not skilled enough to explain you the details but you can't assign a struct directly, you need to create a new one. – FLX Mar 31 '17 at 11:50
  • But don't forget to move elapsedTime as well because it's wrong – FLX Mar 31 '17 at 11:51
  • @FLX Ok I'll dig into that, thanks bro. – armnotstrong Mar 31 '17 at 11:52
  • @FLX @rs232 I made a silly mistake that in the last line I change the `localPosition` of `transform` instead of `obj.transform` , that's why it will not tweak the position precisely, but your comment of move elapsedTime before the movement did help. Thanks again and sorry for the trouble, will delete this question. – armnotstrong Mar 31 '17 at 13:05
  • Ok, nice then. I was wrong about assignation, apparently it's something about struct properties and not the struct itself, unrelated – FLX Mar 31 '17 at 16:39

0 Answers0