0

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)

Game Player Transform

Player Transform

Player Rigidbody

Player Rigidbody

[EDIT] I tried the snap threshold from 5 - 100 in every Combination between ball and Player. The Rigidbody Looks the same (except the mass) on the ball. The balls mass is 0,5. The Player is a prefab so every Player has the same physics. I also played around with the game physics especially the bounce threshold, but it didn't help.

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);
    }
    Vector3 pos = transform.position;

    pos.y += 10;
    Camera.main.transform.position = pos;
}

void FixedUpdate()
{
    if (!isLocalPlayer)
        return;

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

Game Physics

Game Phyiscs

AlexS
  • 21
  • 6

1 Answers1

0

As I understand it, movements are either only played locally (when there is no active NetworkTransform component) or they are synced to the server and then to other clients. So the physics engine has no direct interaction with the networking system, it only changes the transform/rigidbody which is then synced using the NetworkTransform. Thus, what you're trying to do should work.

To answer your question, I would try to play around with the settings in the NetworkTransform of the ball. I just recently had a similar problem and solved it with the following settings:

Network Send Rate: 9
Transfrom Send Mode: Sync Rigidbody 3D

Movement Threshold: 0.001
Snap Threshold: 100
Interpolate Movement Factor: 1

Rotation Axis: XYZ (full 3D)
Interpolate Rotation Factor: 8
Compress Rotation: None
Sync Angular Velocity: No

I'd guess setting the Snap Threshold to something higher, like 100, should solve your issue.

From the docs:

If a movement update puts an object further from its current position that this value, it will snap to the position instead of moving smoothly.

Linus
  • 111
  • 4
  • Hello, sorry that didn't solve the issue. The collision stays some Kind of sticky. – AlexS Jun 13 '16 at 13:10
  • I forgot to mention that I'm talking about the NetworkTransform on your ball. Additionaly, each player should have a NetworkTransform like the one you posted for Player1. Then, of course, all of them need a rigidbody and a collider for which the standard settings should suffice. It's working fine for me with those settings. Maybe setting the global gravitation to 0 in `Edit > Project Settings > Physics` helps? – Linus Jun 14 '16 at 16:06
  • I tried this without success. I updated my question. Maybe you can post your settings of the rigidbody and the Networktransform for your Player and the ball? Thanks! – AlexS Jun 15 '16 at 13:43
  • I just tried to rebuild your project and now I know exactly what you mean by sticky behaviour. I tried many combinations of the settings in the NetworkTransform and couldn't get it to work either, sorry... But I'm pretty sure it should be working with similar settings to yours, maybe give it another try. In case I find a solution, I will let you know. – Linus Jun 16 '16 at 17:59
  • Ok! Thanks for trying! If I find a solution I will post it ;) – AlexS Jun 17 '16 at 09:18