- Home /
Why does my server show different position for my client?
Hi, I am trying to spawn my player at a random location on the plane. I was following the tutorial of Dilmer from Youtube (https://www.youtube.com/watch?v=GOtE96OKyVA&list=PLQMQNmwN3FvyyeI1-bDcBPmZiSaDMbFTi∈dex=3).
So here is the problem: When the client connects it sets a random position on the plane to be spawned on. The problem is that the player always spawns on the position that the prefab has which is (0,0,0) and when another client connects it bugs out because it is on the same position as another client.
Here is the player control class:
using Unity.Netcode; using UnityEngine;
[RequireComponent(typeof(NetworkObject))] public class PlayerControl : NetworkBehaviour { public enum PlayerState { Idle, Walk, ReverseWalk, }
[SerializeField]
private float speed = 3.5f;
[SerializeField]
private float rotationSpeed = 3.5f;
[SerializeField]
private Vector2 defaultInitialPositionOnPlane = new Vector2(-15, 15);
[SerializeField]
private NetworkVariable<Vector3> networkPositionDirection = new NetworkVariable<Vector3>();
[SerializeField]
private NetworkVariable<Vector3> networkRotationDirection = new NetworkVariable<Vector3>();
[SerializeField]
private NetworkVariable<PlayerState> networkPlayerState = new NetworkVariable<PlayerState>();
private CharacterController characterController;
// client caches positions
private Vector3 oldInputPosition = Vector3.zero;
private Vector3 oldInputRotation = Vector3.zero;
private Animator animator;
private void Awake()
{
characterController = GetComponent<CharacterController>();
animator = GetComponent<Animator>();
}
void Start()
{
if (IsClient && IsOwner)
{
transform.position = new Vector3(Random.Range(defaultInitialPositionOnPlane.x, defaultInitialPositionOnPlane.y), 0,
Random.Range(defaultInitialPositionOnPlane.x, defaultInitialPositionOnPlane.y));
}
}
void Update()
{
if (IsClient && IsOwner)
{
ClientInput();
}
ClientMoveAndRotate();
ClientVisuals();
}
private void ClientMoveAndRotate()
{
if (networkPositionDirection.Value != Vector3.zero)
{
characterController.SimpleMove(networkPositionDirection.Value);
}
if (networkRotationDirection.Value != Vector3.zero)
{
transform.Rotate(networkRotationDirection.Value, Space.World);
}
}
private void ClientVisuals()
{
if (networkPlayerState.Value == PlayerState.Walk)
{
animator.SetFloat("Walk", 1);
}
else if (networkPlayerState.Value == PlayerState.ReverseWalk)
{
animator.SetFloat("Walk", -1);
}
else
{
animator.SetFloat("Walk", 0);
}
}
private void ClientInput()
{
// y axis client rotation
Vector3 inputRotation = new Vector3(0, Input.GetAxis("Horizontal"), 0);
// forward & backward direction
Vector3 direction = transform.TransformDirection(Vector3.forward);
float forwardInput = Input.GetAxis("Vertical");
if (Input.GetKey(KeyCode.LeftShift) && forwardInput > 0) forwardInput = 2;
Vector3 inputPosition = direction * forwardInput;
if (oldInputPosition != inputPosition ||
oldInputRotation != inputRotation)
{
oldInputPosition = inputPosition;
UpdateClientPositionAndRotationServerRpc(inputPosition * speed, inputRotation * rotationSpeed);
}
if (forwardInput > 0 && forwardInput <= 1)
{
UpdatePlayerStateServerRpc(PlayerState.Walk);
}
else if (forwardInput < 0)
{
UpdatePlayerStateServerRpc(PlayerState.ReverseWalk);
}
else
{
UpdatePlayerStateServerRpc(PlayerState.Idle);
}
}
[ServerRpc]
public void UpdateClientPositionAndRotationServerRpc(Vector3 newPosition, Vector3 newRotation)
{
networkPositionDirection.Value = newPosition;
networkRotationDirection.Value = newRotation;
}
[ServerRpc]
public void UpdatePlayerStateServerRpc(PlayerState state)
{
networkPlayerState.Value = state;
}
}
How can I spawn a player randomly on the new Unity MLAPI? Shouldn't the player get the random position set by the client? The prefab has NetworkObject, NetworkTransform scripts.
Your answer
Follow this Question
Related Questions
Transform.parent not set on clients UNET 0 Answers
Networking: How to spawn dynamically created objects 2 Answers
UNET 5.2: How do you use NetworkServer.SpawnWithClientAuthority()? 1 Answer
Question about Unet and spawning 0 Answers
New Unity 5.1 Networking :- Spawning is not synchronized!!!! 2 Answers