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
  3. Dismiss Notice

Resolved Client is spawning but Host isn't

Discussion in 'Netcode for GameObjects' started by nargis23, May 16, 2024.

  1. nargis23

    nargis23

    Joined:
    Jul 14, 2023
    Posts:
    8
    Hello everyone,

    I'm a new developer currently working with Netcode for a multiplayer game. I'm trying to implement a feature in my third-person shooter (TPS) where all players (clients) spawn from a certain height, creating the effect of players descending from the sky. I'm using two variables, positionRangeUpwards and positionRangeUpwards2, to determine the range of the spawn height.

    However, I'm encountering a few issues:

    1. When I enter the game as a host, the falling doesn't work, although it works correctly for the clients.
    2. When playing with exactly four players, the spawn behavior becomes erratic. Some players spawn from the sky as intended, while others spawn directly on the ground.
    I'd appreciate any advice or suggestions on how to resolve these problems. Thank you!

    Note: I've implemented that logic in the UpdatePosServerRpc() and which is handled in the OnNetworkSpawn().

    The code is:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using NUnit.Framework;
    4. using Unity.Netcode;
    5. using UnityEngine;
    6.  
    7. public class ThirdpersonMovementNet : NetworkBehaviour
    8. {
    9.     public CharacterController controller;
    10.     public Transform cam;
    11.     public float speed = 6f;
    12.     public float turnSmoothTime = 0.1f;
    13.     private float turnSmoothVelocity;
    14.  
    15.     [SerializeField] private float positionRange = 3f;
    16.     [SerializeField] private float positionRangeUpwards = 1082.0626f;
    17.     [SerializeField] private float positionRangeUpwards2 = 682.0626f;
    18.  
    19.     [SerializeField] private float gravity = -9.81f;
    20.     [SerializeField] private float jumpHeight = 1.0f;
    21.     private Vector3 velocity;
    22.     private bool isGrounded;
    23.  
    24.    
    25.     public Transform groundCheck;
    26.     public float groundDistance = 0.4f;
    27.     public LayerMask groundMask;
    28.  
    29.  
    30.     public override void OnNetworkSpawn()
    31.     {
    32.  
    33.         if (IsClient || IsHost)
    34.         {
    35.             UpdatePosServerRpc();
    36.         }
    37.  
    38.  
    39.  
    40.     }
    41.  
    42.    
    43.     private void Start()
    44.     {
    45.  
    46.         Cursor.lockState = CursorLockMode.Locked;
    47.        
    48.     }
    49.     void Update()
    50.     {
    51.         if (!IsOwner) return;
    52.  
    53.  
    54.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    55.  
    56.         if (isGrounded && velocity.y < 0)
    57.         {
    58.             velocity.y = -2f;
    59.         }
    60.  
    61.         float horizontal = Input.GetAxisRaw("Horizontal");
    62.         float vertical = Input.GetAxisRaw("Vertical");
    63.  
    64.         Vector3 dir = new Vector3(horizontal, 0f, vertical).normalized;
    65.  
    66.         if (dir.magnitude >= 0.1f)
    67.         {
    68.             float targetAngle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    69.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    70.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
    71.             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    72.             controller.Move(moveDir.normalized * speed * Time.deltaTime);
    73.         }
    74.  
    75.         if (isGrounded && Input.GetButtonDown("Jump"))
    76.         {
    77.             velocity.y = Mathf.Sqrt(jumpHeight * -6f * gravity);
    78.         }
    79.  
    80.         velocity.y += gravity * Time.deltaTime;
    81.         controller.Move(velocity * Time.deltaTime);
    82.     }
    83.  
    84.     [ServerRpc(RequireOwnership = false)]
    85.     private void UpdatePosServerRpc()
    86.     {
    87.         transform.position = new Vector3(Random.Range(positionRange, -positionRange), Random.Range(positionRangeUpwards, positionRangeUpwards2), Random.Range(positionRange, -positionRange));
    88.         transform.rotation = Quaternion.Euler(0, 180, 0);
    89.     }
    90.  
    91. }
    92.  
     
    Last edited: May 16, 2024