Colyseus smooth cube movement?

Hi,
Colyseus has an MMO example that I was trying to simplify down to have only 2 cubes to move in sync.
I nearly achieved it by:

  1. In FixedUpdate() commenting out ProcessViewSync()
  2. changing SyncViewWithServer(), and adding a new CoroutineExample() to move the NPC (cube) by Vector3.MoveTowards()
  3. having a dead simple cube-move implementation for the cube network player using transform.Translate()

I will bring here the code and the video of cubes moving. As you can see, if to move the cube by diagonal (say, press and hold both left and up arrows on the keyboard), the NPC cube moves smoothly (acceptable), otherwise, it stops at the target point and then starts to move toward the next target. How to have it not to stop if the List contains more than 1 target position, please?

video:

Updated code for NetworkPlayerEntity.cs (Colyseus MMO example):

    private void SyncViewWithServer()
    {
        Vector3 pos = new Vector3((float)state.xPos, (float)state.yPos, (float)state.zPos);
        Quaternion rot = new Quaternion((float)state.xRot, (float)state.yRot, (float)state.zRot, (float) state.wRot);
        transform.localRotation = rot;
        targetPos.Add(pos);
        if(!isMoveCoroutineRunning)
            StartCoroutine("CoroutineExample");
    }
    IEnumerator CoroutineExample()
    {
        isMoveCoroutineRunning = true;
        Vector3 tmpPos = Vector3.zero;
        float mvSpeed = 6f;// CubeMove speed + 1f = 6f
        bool doMove = true;
        while (doMove){
            transform.localPosition = Vector3.MoveTowards(transform.localPosition, targetPos[0], Time.deltaTime * mvSpeed);
            if(Vector3.Distance(transform.localPosition, targetPos[0]) <= 0.01f)
            {
                tmpPos = targetPos[0];
                targetPos.RemoveAt(0);
                if(targetPos.Count == 0)
                {
                    doMove = false;
                    transform.localPosition = tmpPos;
                }
            }
            yield return null;
        }
        yield return null;
        isMoveCoroutineRunning = false;
    }

Code for moving cube player:

public class CubeMove : MonoBehaviour
{
    float horiz;
    float vert;
    float mvSpeed = 5f;
    void Start()
    {
     
    }

    void Update()
    {
        horiz = -Input.GetAxis("Horizontal") * Time.deltaTime * mvSpeed;
        vert = -Input.GetAxis("Vertical") * Time.deltaTime * mvSpeed;
        transform.Translate(horiz, 0f, vert);
    }
}

I’m not 100% on this, but could be a problem in this part:

        yield return null; // <--- REMOVE THIS (line 21)
        isMoveCoroutineRunning = false;

When you leave the loop and yield before setting the “isMoveCoroutineRunning” to false, you return to your normal script execution. If in this normal execution you try to queue another movement: when executing “SyncViewWithServer”, you add a movement to the queue, but since “isMoveCoroutineRunning” still true, it wont start a new coroutine and that last coroutine will just continue to die.

Thank you for the response. I googled for “yield return null”, quoting the Unity Forum post: "
Yield return null inside a coroutine will wait for the next frame."

I commented out the line and things are the same. But even if to leave it, as the next frame finishes the coroutine and as points are continuously being added, it will restart the coroutine anyway. The interesting thing is that if only dx or dy is contributing to the movement (by only pressing and holding, say, up, or right arrow, but “not” both) the partial movement is present, but not when both dx and dy are being contributed when holding up and right arrow “together pressed”.

OK, if you have a github for these scripts, we could take a look on the whole code.
What i can suggest is to debug it in small steps. As we can see you are working with some kind of queue in “targetPos” list, so first step is to check if your movements are getting queued properly. In the “SyncViewWithServer” method, print the “pos”, check if the position coming from state is getting affected by pressing both buttons or not. If not, the problem is before it, you’ll need to track what’s creating(modifying) the state.

1 Like

Thanks, I will try these, btw, code is already available and you can apply my changes from above snippets:
https://docs.colyseus.io/colyseus/demo/mmo/