Hi!
I am working with netcode objects and I am having some trouble making it work.
I have developed a Host-Client logic, and the clients themselves handle the player’s input system and move/rotate it. For this I am using Client Network Transform.
All of that is Ok but the problem comes when I build and start testing.
1st I create the host and it does work perfectly as expected.
Then I create a client (This can not be moved and does not react to any input)
Afterwards I create a client and I can move this one.
If I close the first client and then create another one, it does not respond even.
What can it be with my problem? I am leaving the player’s prefab attached in this post and this is the code:
using UnityEngine;
using Unity.Netcode;
using UnityEngine.InputSystem;
using Cinemachine;
public class PlayerController : NetworkBehaviour {
Player player;
private Vector2 direction;
float cameraRotation;
[SerializeField] private float sensitivity = 0.1f;
[SerializeField] private CinemachineVirtualCamera vc;
[SerializeField] private AudioListener listener;
[SerializeField] private float maxUseDistance;
[SerializeField] private LayerMask useLayers;
const float RUNNING_SPEED = 1500f;
const float WALKING_SPEED = 500f;
private float speed = WALKING_SPEED;
public void Awake()
{
this.player = new Player(gameObject);
}
public override void OnNetworkSpawn()
{
if (IsOwner)
{
listener.enabled = true;
vc.Priority = 1;
}
else
{
vc.Priority = 0;
}
}
private void Update()
{
if (!IsOwner) { return; }
this.player.RotateY(this.cameraRotation);
}
private void FixedUpdate()
{
if (!IsOwner) { return; }
Debug.Log("Moving: "+ this.direction.x + "speed: " + this.speed);
this.player.Move(this.direction, this.speed);
}
public void OnMovement(InputAction.CallbackContext ctx)
{
if (!IsOwner) { return; }
this.direction = ctx.ReadValue();
}
public void OnCameraRotation(InputAction.CallbackContext ctx)
{
if (!IsOwner) { Debug.Log(“NOT OWNER”); return; }
this.cameraRotation = ctx.ReadValue().x * this.sensitivity;
}
public void OnRun(InputAction.CallbackContext ctx)
{
if (!IsOwner) { return; }
float newSpeed = this.speed;
if (ctx.performed) {
newSpeed = RUNNING_SPEED;
} else if (ctx.canceled)
{
newSpeed = WALKING_SPEED;
}
this.speed = newSpeed;
}
public void OnUse(InputAction.CallbackContext ctx)
{
if (!IsOwner) { return; }
if (ctx.performed)
{
Vector3 centerColliderPos = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y + 1f, this.gameObject.transform.position.z);
if (Physics.Raycast(centerColliderPos, this.gameObject.transform.forward, out RaycastHit hit, maxUseDistance, useLayers))
{
if (hit.collider.TryGetComponent(out DoorController door))
{
door.Interact(this.gameObject);
}
}
Collider[ ] hitColliders = Physics.OverlapBox(this.gameObject.transform.position, this.gameObject.transform.localScale * 3, Quaternion.identity, useLayers);
for (int i = 0; i < hitColliders.Length; i++)
{
if (hitColliders*.TryGetComponent(out ItemController item))*
{
item.AddToInventory(player.GetInventory());
}
}
}
}
public Player GetPlayer()
{
return this.player;
}
}