- Home /
Multiplayer collision glitches
I try to make a simple multiplayer example. It doesn't matter if i try it in 2d or in 3d the result is the same. I will describe it in 3D that's what I tried a least. I simple add two cylinders to a Scene and a ball. I set up a script for the Player Controller so each Player can controll his own Player. I add the Network transform, the network ID set it to local playre authority for the Player prefab and did everything describe in the several tutorials I red.
Thinks are working as expected. On the host I can collide with the Ball and the other Player, after adding a bouncing material the ball is bouncing away from the Player and also the other Player does, when I hit him hard enough. (I added a boost on the fire button) I did not program the collisions, I just let the physics engine do the work. The Problem is on the Client side, there the Ball behave some Kind of sticky. It doesn't bounces of the Player it is glitchy and behaves just not the right way.
I tried every combination with Collision Detection Descrete - Continuous - Continuous Dynmics with Interpolation or without or Extrapolation. Combination between Player and Ball and Player / Player.
I also tried different combination with the Network Settings, but nothing helps.
I add some screenshots, so you have a better understandig of what I try to do and my Settings.
The Questions is: Is the pyhics engine in combination with Network capable, to do what I want, or is the better way to: Programm the pyhics OR put the Player from the Client back to the Server and let him just remote control from the Client. I think of catch the Input from the Keyboard and sending it to the Server and do the movment there and sync back only the graphics. (I hope, that you get what I mean)
Player Script:
using UnityEngine; using UnityEngine.Networking; using System.Collections; using UnityStandardAssets.CrossPlatformInput;
public class PlayerController : NetworkBehaviour {
public float movmentForce = 75;
public float accelerationOnFire;
public GameObject ball;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
private Rigidbody rbPlayer;
// Use this for initialization
void Start()
{
Vector3 pos;
Quaternion q;
pos.x = 0;
pos.y = 1;
pos.z = 10;
transform.position = pos;
rbPlayer = GetComponent<Rigidbody>();
ball = GameObject.FindGameObjectWithTag("Ball");
}
// Update is called once per frame
void Update()
{
if (!isLocalPlayer)
return;
float inputHorizontal = CrossPlatformInputManager.GetAxis("Horizontal");
float inputVertical = CrossPlatformInputManager.GetAxis("Vertical");
// float inputHorizontal = Input.GetAxis("Horizontal");
// float inputVertical = Input.GetAxis("Vertical");
if (inputHorizontal != 0)
{
rbPlayer.AddForce(Vector3.right * movmentForce * inputHorizontal, ForceMode.Force);
}
if (inputVertical != 0)
{
rbPlayer.AddForce(Vector3.forward * movmentForce * inputVertical, ForceMode.Force);
}
if (CrossPlatformInputManager.GetButton("Fire1") && Time.time > nextFire)
// if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
rbPlayer.AddForce(transform.forward * -accelerationOnFire, ForceMode.Force);
}
//Center the camera over the player
Vector3 pos = transform.position;
pos.y += 10;
Camera.main.transform.position = pos;
}
void FixedUpdate()
{
if (!isLocalPlayer)
return;
//Rotates the Player towards the ball, so the Player faces the ball
if (ball)
{
Vector3 dir = ball.transform.position - rbPlayer.transform.position;
float angle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg;
rbPlayer.transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
rbPlayer.transform.Rotate(0, 180, 0);
}
else
{
ball = GameObject.FindGameObjectWithTag("Ball");
}
}
}
Ball Spawner:
using UnityEngine; using UnityEngine.Networking; using System.Collections;
public class BallSpawn : NetworkBehaviour {
public GameObject ballPrefab;
// Use this for initialization
public override void OnStartServer()
{
SpawnBall();
}
void SpawnBall()
{
GameObject myBall = Instantiate<GameObject>(ballPrefab);//, Vector2.zero, Quaternion.identity);
NetworkServer.Spawn(myBall);
myBall.tag = "Ball";
}
}