Search Unity

Bug Client can't move, shoot or move camera horizontally

Discussion in 'Multiplayer' started by HelloThere65, Jan 27, 2024.

  1. HelloThere65

    HelloThere65

    Joined:
    Aug 23, 2022
    Posts:
    13
    Hi all,

    Im making a very simple gun game, and the clients who join cannot move, and for some reason the client can only move their camera vertically, and they can call 'Shoot()' (since the animation plays), yet the raycast doesnt work either. Here is my player controller:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using Unity.Netcode;
    4.  
    5. public class PlayerController : NetworkBehaviour
    6. {
    7.     public float movementSpeed = 5f;
    8.     public float rotationSpeed = 2f;
    9.     public float maxLookUpAngle = 80f;
    10.     public float maxLookDownAngle = 80f;
    11.     public float gravity = 2f;
    12.     public float jumpForce = 8f;
    13.     public float mouseSensitivity = 4f;
    14.  
    15.     public NetworkVariable<int> health = new NetworkVariable<int>(100);
    16.  
    17.     public int damage = 10;
    18.  
    19.     public Transform gunPoint;
    20.  
    21.     bool attackCooldown = false;
    22.  
    23.     public Animator gunAnim;
    24.     public Camera cam;
    25.     public AudioListener aud;
    26.     public AudioSource audioSource;
    27.  
    28.     private CharacterController characterController;
    29.     private float verticalRotation = 0f;
    30.     private Vector3 velocity;
    31.  
    32.     void Start()
    33.     {
    34.         characterController = GetComponent<CharacterController>();
    35.         Cursor.lockState = CursorLockMode.Locked;
    36.         Cursor.visible = false;
    37.     }
    38.  
    39.     void Update()
    40.     {
    41.        
    42.             if (Input.GetKeyDown(KeyCode.Mouse0) && IsLocalPlayer)
    43.             {
    44.                 if (attackCooldown == false)
    45.                 {
    46.                     StartCoroutine(Shoot());
    47.                 }
    48.             }
    49.  
    50.         if (IsLocalPlayer)
    51.         {
    52.             // Player movement with WASD
    53.             float horizontalInput = 0f;
    54.             float verticalInput = 0f;
    55.  
    56.             if (Input.GetKey(KeyCode.W))
    57.             {
    58.                 verticalInput = 1f;
    59.             }
    60.             else if (Input.GetKey(KeyCode.S))
    61.             {
    62.                 verticalInput = -1f;
    63.             }
    64.  
    65.             if (Input.GetKey(KeyCode.D))
    66.             {
    67.                 horizontalInput = 1f;
    68.             }
    69.             else if (Input.GetKey(KeyCode.A))
    70.             {
    71.                 horizontalInput = -1f;
    72.             }
    73.  
    74.             Vector3 movement = new Vector3(horizontalInput, 0f, verticalInput) * movementSpeed * Time.deltaTime;
    75.             Vector3 rotatedMovement = transform.TransformDirection(movement);
    76.  
    77.             // Combine movement and gravity
    78.             characterController.Move(rotatedMovement + velocity * Time.deltaTime);
    79.  
    80.             // Player rotation (looking around)
    81.             float mouseX = Input.GetAxisRaw("Mouse X") * mouseSensitivity;
    82.             float mouseY = Input.GetAxisRaw("Mouse Y") * mouseSensitivity;
    83.  
    84.             // Rotate around the Y-axis (left and right)
    85.             transform.Rotate(Vector3.up * mouseX * rotationSpeed * Time.deltaTime);
    86.  
    87.             // Rotate around the X-axis (up and down) with clamping
    88.             verticalRotation -= mouseY * rotationSpeed * Time.deltaTime;
    89.             verticalRotation = Mathf.Clamp(verticalRotation, -maxLookDownAngle, maxLookUpAngle);
    90.  
    91.             cam.transform.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);
    92.  
    93.             // Apply gravity
    94.             if (characterController.isGrounded)
    95.             {
    96.                 velocity.y = -0.5f; // Reset vertical velocity when grounded
    97.  
    98.                 // Jumping
    99.                 if (Input.GetKeyDown(KeyCode.Space))
    100.                 {
    101.                     velocity.y = Mathf.Sqrt(jumpForce * 2f * gravity);
    102.                 }
    103.             }
    104.             else
    105.             {
    106.                 velocity.y -= gravity * Time.deltaTime;
    107.             }
    108.         }
    109.  
    110.         if (characterController.isGrounded)
    111.         {
    112.             velocity.y = -0.5f; // Reset vertical velocity when grounded
    113.  
    114.             // Jumping
    115.             if (Input.GetKeyDown(KeyCode.Space) && IsLocalPlayer)
    116.             {
    117.                 velocity.y = Mathf.Sqrt(jumpForce * 2f * gravity);
    118.             }
    119.         }
    120.         else
    121.         {
    122.             velocity.y -= gravity * Time.deltaTime;
    123.         }
    124.     }
    125.  
    126.     IEnumerator Shoot()
    127.     {
    128.         gunAnim.SetTrigger("Shoot");
    129.         yield return new WaitForSeconds(0.2f);
    130.         audioSource.Play();
    131.         if (Physics.Raycast(transform.position, transform.forward, out RaycastHit hitInfo, 10))
    132.         {
    133.             hitInfo.transform.GetComponent<PlayerController>().TakeDamage(damage);
    134.         }
    135.         attackCooldown = true;
    136.         yield return new WaitForSeconds(0.2f);
    137.         attackCooldown = false;
    138.     }
    139.  
    140.     void TakeDamage(int damage)
    141.     {
    142.         health.Value -= damage;
    143.         if (health.Value <= 0)
    144.         {
    145.             Destroy(gameObject);
    146.         }
    147.     }
    148. }
     
  2. HelloThere65

    HelloThere65

    Joined:
    Aug 23, 2022
    Posts:
    13
    also im using Unity, it is a 3D game, and for networking im using Netcode for gameobjects and Relay
     
  3. HelloThere65

    HelloThere65

    Joined:
    Aug 23, 2022
    Posts:
    13
    i have fixed the movement errors. i added a client network transform