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:
- In FixedUpdate() commenting out ProcessViewSync()
- changing SyncViewWithServer(), and adding a new CoroutineExample() to move the NPC (cube) by Vector3.MoveTowards()
- 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);
}
}