Refining player movement

So now that my changes to the background tiles went through, the next thing I want to work on is to add some animation to the player movement.  There are a couple things I need to change in order to make it work. First, I needed to make some more sprites for the animation. I’m still not an artist so am utilizing programmer art once again and the animations are a animated arrows pointing in the direction of movement.

player_animations

Second,I need to smooth out the movement. Right now players basically teleport from one square to another. So I need to change from editing the position to using the translate method.

The reason I did not originally use translate was the code ensuring the sprite stopped at the point I wanted was more complex. But I felt like now was a better time to try and tackle it. So here is my new move function.

 


 protected IEnumerator SmoothMovement(Vector3 destination)
{
while(Vector3.Distance(transform.position, destination) > 0)
{
transform.Translate((destination - transform.position) * Speed * Time.deltaTime);
if (Vector3.Distance(transform.position, destination) < 0.01)
Move(destination - transform.position);
yield return null;
}

EnablePlayerInput = true; // Input was disabled when input was collected
}

As evident by the IEnumerator type, The new movement function is what Unity calls a Coroutine. It works by moving a small distance to each frame and fixes itself to the correct location when the distance is less than 0.01. I don’t know if I really like the last part, but for now it is working well enough.animated

Lastly I added the new sprites and animation clips into. Here is the sprite at rest on the left and a move right animation on the right.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s