Sign in to follow this  

Unity Moving 2D Sprite Not Smooth

This topic is 543 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I am starting out trying to create a top down 2D game that has grid based movement (like Pokemon) I have an implementation of movement functional however it is not really smooth:

 

giphy.gif

 

It seems to be janky when it stops the current MovePosition and starts the next one. What can I do to make this transition smooth while the key is being pressed?

Here is the code:

using UnityEngine;
using System.Collections;
 
public class PlayerController : MonoBehaviour {
    public float speed = 5f;
 
    private bool isMoving = false;
    private BoxCollider2D boxCollider;
    private Rigidbody2D rigidbody;
 
    void Start() {
        boxCollider = GetComponent<BoxCollider2D>();
        rigidbody = GetComponent<Rigidbody2D>();
    }
 
    void FixedUpdate() {
        if (!isMoving) {
            bool moveLeft = Input.GetKey(KeyCode.A);
            bool moveDown = Input.GetKey(KeyCode.S);
            bool moveRight = Input.GetKey(KeyCode.D);
            bool moveUp = Input.GetKey(KeyCode.W);
 
            if (moveLeft || moveDown || moveUp || moveRight) {
                Vector3 move = new Vector3(0, 0, 0);
 
                if (moveLeft) {
                    move.x = -1;
                }
 
                if (moveRight) {
                    move.x = 1;
                }
 
                if (move.x == 0) {
                    if (moveUp) {
                        move.y = 1;
                    }
 
                    if (moveDown) {
                        move.y = -1;
                    }
                }
 
                Vector3 end = transform.position + move;
 
                boxCollider.enabled = false;
                RaycastHit2D hit = Physics2D.Linecast(transform.position, end);
                boxCollider.enabled = true;
 
                if (hit.transform == null) {
                    StartCoroutine(PerformMove(end));
                }
            }
        }
    }
 
    protected IEnumerator PerformMove(Vector3 end) {
        isMoving = true;
        float squareRemainingDistance = (transform.position - end).sqrMagnitude;
 
        while (squareRemainingDistance > 0f) {
            Vector3 newPosition = Vector3.MoveTowards(rigidbody.position, end, speed * Time.deltaTime);
 
            rigidbody.MovePosition(newPosition);
 
            squareRemainingDistance = (transform.position - end).sqrMagnitude;
            yield return null;
        }
 
        isMoving = false;
    }
}

Share this post


Link to post
Share on other sites

I think this is probably happening because you are kicking it off from FixedUpdate() and there is some aliasing going on between when FixedUpdate() is called and when the coroutine is pumped. I would throw some Debug.Log calls into FixedUpdate(), Update() and PerformMove() so you can see what is being called when. The key is that you need to make sure PerformMove() is getting reentered on the same frame it sets isMoving to false or you will have stutter.

Edited by BrainDx

Share this post


Link to post
Share on other sites

I think this is probably happening because you are kicking it off from FixedUpdate() and there is some aliasing going on between when FixedUpdate() is called and when the coroutine is pumped. I would throw some Debug.Log calls into FixedUpdate(), Update() and PerformMove() so you can see what is being called when. The key is that you need to make sure PerformMove() is getting reentered on the same frame it sets isMoving to false or you will have stutter.

 

Well the calls are happening in the correct order however I am not sure how to check if PerformMove is being called within the same frame that isMoving is being set to false.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Similar Content

    • By loyalsheep
      Hey guys!
      Check out the trailer of Airavat. It's a minimalist, space survival game (and hard, very hard) 
      Download on GooglePlay: Airavat
       
    • By anomalystudiospt
      Hey, we are new around here!
      Our team spent the last year developing a prototype game called Rise of The Fey, a game that focuses on a dramatic story with a fast paced gameplay.
      Rise of The Fey is a third-person action-adventure RPG set within a fantasy world called Perrah. In Rise you control Kall, a young spaunian looking for revenge after the death of his beloved.

      We would appreciate if you could take a look at our stuff, tell us what you think and possibly help us out on Indiegogo.
      The prototype is in portuguese, sorry about that!
      Have a good one!
    • By juglar
      Hi guys, we are developing a game called BiteOff! based on Zombies topic. BiteOff! is based on a game strategy mechanics, free to play, where the player has a bunch of zombies and he can distribute them along the map landscape to be able to bite and transform all humans into each level. Humans are controlled by artificial intelligence and they differ each other on capabilities based on different weapons that give them three different levels of aggressiveness to defend against the zombies. The zombies are also presented in three types and they are differentiated by their attack, speed, strength and resistance abilities. Through different achievements, (time to eat all humans, score, among others) the player can access four extra skills that can enhance your attack in real time on next levels as power-ups.
      We are launching a crowdfunding campaign in kickico and we need some helps please visit us on the crowdfunding page https://www.kickico.com/es/campaigns/17824/biteoff-the-videogame 
      Twitter @biteoffthegame
      Facebook
      Clickme!
      Website 
      Clickme!
    • By Lord McMutton
      The world of Ethios is held aloft by massive trees known as the Pillars.
  • Popular Now