Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Client Transform - 1st client does not respond and is not owner

Discussion in 'Netcode for GameObjects' started by Bata340, Sep 18, 2023.

  1. Bata340

    Bata340

    Joined:
    Sep 8, 2023
    Posts:
    2
    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<Vector2>();
    }


    public void OnCameraRotation(InputAction.CallbackContext ctx)
    {
    if (!IsOwner) { Debug.Log("NOT OWNER"); return; }
    this.cameraRotation = ctx.ReadValue<Vector2>().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;
    }
    }
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      69.8 KB
      Views:
      43
    • 2.jpg
      2.jpg
      File size:
      48 KB
      Views:
      43
    • 3.jpg
      3.jpg
      File size:
      45.4 KB
      Views:
      43
    • 4.jpg
      4.jpg
      File size:
      58.5 KB
      Views:
      43
    • NM.jpg
      NM.jpg
      File size:
      86.7 KB
      Views:
      43
  2. Bata340

    Bata340

    Joined:
    Sep 8, 2023
    Posts:
    2
    Solved using OnNetworkSpawn with this code:

    if (!IsOwner) { gameObject.GetComponent<PlayerInput>().enabled = false; }

    And afterwards removing the if (IsOwner) in the controller callback events :)
     
  3. JethRow

    JethRow

    Joined:
    Sep 22, 2021
    Posts:
    10
    You can also just call if(!IsOwner) {this.enabled=false} in the start or the awake within scripts so u dont have to get components