How do I get physics ball collision to look smooth in a multiplayer game?
I am working on a multiplayer game right now and each player is a ball. When a player is hit they are pushed back, with the objective of pushing that player off of the edge of the arena. I have collision working and here is some of my code. The script below is attatched to the player prefab, which is a ball. The script checks to see WHEN THERE IS A COLLISION whoever has a greater magnitude and velocity behind them then hit the other player which just activates the last bit of code on the player that had less. so addForce is added to the localplayer every time. I dont know if this is the best way or not. It works, but over the network it is a little laggy when hit back. the local players ball looks great when hit, but the other players position is just not clean if the hitpower is high enough it will pretty much look like the player is teleported back. Any help would be appreciated. Thanks.
{
public float hitpower;
private Rigidbody rb;
private GameObject other;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player" && rb.velocity.magnitude < collision.rigidbody.velocity.magnitude && isLocalPlayer)
{
//if (rb.velocity.magnitude > collision.rigidbody.velocity.magnitude)
Debug.Log("Ball has more power");
rb.AddForce(collision.contacts[0].normal * collision.relativeVelocity.magnitude * hitpower, ForceMode.Impulse);
Debug.Log("this ball was hit");
other = collision.gameObject;
}
}
}
Your answer