c# - Moving the player tile by tile smoothly in Unity -
i want make player move specific amount (1 tile/unit in case) , stop him. in video:
http://www.youtube.com/watch?v=dotoabnngc4
by pressing key player should move smoothly next tile, stops , on.
how can archieve this? (in c#)
here's do: give player class target field, e.g. 2d vector. either contain current position of player (when initializing or when target has been reached) or target want move player to.
now in update this:
if target equals current position check user input movement. if user requests movement set target position adjacent current position. else move player towards target.
you may want create field current position of player besides normal position in gameobject. update gameobject.position in update-cylce small delta until positions of gameobject.position , target have (roughly! use epsilon when comparing) same value.
some hints
- vector2.movetowards can you
- to check if target , position "close enough" subtract 1 other , @ resulting vector's magnitude.
- beware of overshooting when move towards target. if movement distance more or equal distance between position , target, set position target.
Comments
Post a Comment