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;
}
}
}