2

I am coding a animated ball multiplayer mobile game. The ball animation is quite fast (speed around 25-30) and there are multiple balls.

Initially, I tried to the physics are run on masterclient and synced in clients via network. However, the ball animation on the clients are not very smooth which will reduce the player's joy.

Secondly, I tried to run each physics on each client separately with below function, however, the physics simulation is different between the android and unity editor.

Finally, what should I do?

Rigidbody rb;
float t1 = 0.0f;
float t2 = 0.0f;
float scale = 2f;
float maxVelocity = 20f;
bool collided = false;
float limitSpeed = 30f;

float cooldown = 1;

void Start()
{
    rb = GetComponent<Rigidbody>();        
}

void InitialKick()
{
    switch (GameObject.Find("GameManager").GetComponent<GameManager>().ballCount)
    {
        case 1:
            rb.AddForce(new Vector3(20, 0f, 20), ForceMode.Force);
            break;
        case 2:
            rb.AddForce(new Vector3(-20, 0f, 20), ForceMode.Force);
            break;
        case 3:
            rb.AddForce(new Vector3(20, 0f, -20), ForceMode.Force);
            break;
        case 4:
            rb.AddForce(new Vector3(-20, 0f, -20), ForceMode.Force);
            break;
        case 5:
            rb.AddForce(new Vector3(10, 0f, 30), ForceMode.Force);
            break;
        case 6:
            rb.AddForce(new Vector3(-10, 0f, 30), ForceMode.Force);
            break;
        case 7:
            rb.AddForce(new Vector3(10, 0f, -30), ForceMode.Force);
            break;
        case 8:
            rb.AddForce(new Vector3(-10, 0f, -30), ForceMode.Force);
            break;
        case 9:
            rb.AddForce(new Vector3(30, 0f, 10), ForceMode.Force);
            break;
        case 10:
            rb.AddForce(new Vector3(-30, 0f, 10), ForceMode.Force);
            break;
    }
}

private void FixedUpdate()
{
    if (!collided) InitialKick();
    if (collided & rb.velocity.magnitude < maxVelocity) rb.AddForce(3f * scale * rb.velocity.normalized, ForceMode.Force);

    t1 += Time.deltaTime;
    if (t1 > 1.0f)
    {
        scale += 0.5f;
        t1 = 0.0f;
    }

    t2 += Time.deltaTime;
    if (t2 > 10.0f)
    {
        if (maxVelocity < 40f) maxVelocity++;
        t2 = 0.0f;
    }

    rb.mass += Time.deltaTime * 0.01f;
}

private void OnCollisionEnter(Collision collision)
{

    if (collision.collider.tag == "Wall" || collision.collider.tag == "Column")
    {
        collided = true;
    }

}
}
schnaader
  • 49,103
  • 10
  • 104
  • 136
Ztorum
  • 83
  • 11

1 Answers1

1

What I would have done is calculating the physics (a list of the ball positions in the animation for instance) on the masterclient. The masterclient will send the results of the calculations to the each client. Eventually, each client will create an animation from the results.

shahar
  • 355
  • 2
  • 18
  • Thank you Shahar, sounds perfect. Sorry might be a silly question, what do you mean by "calculating the physics"? Actually, I understood what you mean perfectly in theory but what should I do in pratical :) – Ztorum Dec 12 '19 at 22:03
  • 1
    The best thing to is to use the physics calculations at the clients, but I don't know I to do it. You can learn how to do it or you can create an array of the ball positions to create the ball movement animation in the clients. – shahar Dec 14 '19 at 15:58