I am using Mirror API with Unity 2019 to make a basic multiplayer game. The game is a simple ice hockey style with cylinder shaped players and a cylinder puck. The players are spawned by Network Manager as PlayerPrefabs. The puck is a Registered Spawnable Prefab.
They all use Rigidbody for the movements.
In the editor (or single player only game) it works great, but once a client joins the game and hits the puck, the puck moves faster on the client and then teleports (or perhaps rubberbands) to where it is 'supposed' to be.
I've tried several tweaks on the Network Send Intervals for each Identity (ie. 0; 0.1; 0.001; 1/60; 1/64; 1/128 and others). I also changed the "Server Tick rate" to 10; 20; 30; 60; 64; 128.
I'm fairly sure I have all the code in the correct places and calling to the server to make the spawns etc. But below is all the code just in case. But as I say I think the code is ok and that it's something in the settings I need to change. (I already tried the Compression to None, Some , Lots and it seemed best on None). Most of the networked values are 'short'.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Mirror;
public class MatchController : NetworkBehaviour
{
[HideInInspector] public ushort score_Team1 = 0;
[HideInInspector] public ushort score_Team2 = 0;
public Text scoreText;
public GameObject puckPrefab;
[HideInInspector] public GameObject puck;
private void Start()
{
UpdateScoreDisplay();
if (puck == null)
{
SpawnNewPuck();
}
}
public void UpdateScoreDisplay()
{
scoreText.text = score_Team1 + " : " + score_Team2;
}
public void GoalScored(short goalID, uint scorersNetId)
{
if (goalID == 1)
{
score_Team1 += 1;
}
if (goalID == 2)
{
score_Team2 += 1;
}
UpdateScoreDisplay();
}
public void SpawnNewPuck()
{
if (!isServer)
{
return;
}
puck = Instantiate(puckPrefab);
NetworkServer.Spawn(puck);
}
}
and
[RequireComponent(typeof(Rigidbody))]
public class HockeyInput_MP : NetworkBehaviour
{
Rigidbody rb;
public float moveForce;
public float rotSensitivity;
public Transform stickTransform;
private void Start()
{
if (!isLocalPlayer)
{
return;
}
rb = GetComponent();
rb.freezeRotation = true;
Camera.main.GetComponent().target = transform;
}
private void Update()
{
if (!isLocalPlayer)
{
return;
}
DoMovement(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
DoStickMovement(Input.GetAxis("RightStick_Horiz"), Input.GetAxis("RightStick_Vert"));
}
private void DoMovement(float h, float v)
{
Vector3 force = new Vector3(h * moveForce, 0, v * moveForce);
rb.AddForce(force);
}
private void DoStickMovement(float rightStick_h, float rightStick_v)
{
Vector3 rot = stickTransform.localEulerAngles;
rot.y += rightStick_h * rotSensitivity * Time.deltaTime;
stickTransform.localEulerAngles = rot;
}
}
The 'Puck.cs' is currently just an empty class attached the the puck object.
ALSO: I have my Rigidbody components set to Interpolate.
Any ideas how I can make the puck sync'd on both client and host? Any help massively appreciated as always. Many thanks!
No comments:
Post a Comment