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:
using UnityEngine;
using System.Collections;
using Unity.Netcode;
public class PlayerController : NetworkBehaviour
{
public float movementSpeed = 5f;
public float rotationSpeed = 2f;
public float maxLookUpAngle = 80f;
public float maxLookDownAngle = 80f;
public float gravity = 2f;
public float jumpForce = 8f;
public float mouseSensitivity = 4f;
public NetworkVariable<int> health = new NetworkVariable<int>(100);
public int damage = 10;
public Transform gunPoint;
bool attackCooldown = false;
public Animator gunAnim;
public Camera cam;
public AudioListener aud;
public AudioSource audioSource;
private CharacterController characterController;
private float verticalRotation = 0f;
private Vector3 velocity;
void Start()
{
characterController = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0) && IsLocalPlayer)
{
if (attackCooldown == false)
{
StartCoroutine(Shoot());
}
}
if (IsLocalPlayer)
{
// Player movement with WASD
float horizontalInput = 0f;
float verticalInput = 0f;
if (Input.GetKey(KeyCode.W))
{
verticalInput = 1f;
}
else if (Input.GetKey(KeyCode.S))
{
verticalInput = -1f;
}
if (Input.GetKey(KeyCode.D))
{
horizontalInput = 1f;
}
else if (Input.GetKey(KeyCode.A))
{
horizontalInput = -1f;
}
Vector3 movement = new Vector3(horizontalInput, 0f, verticalInput) * movementSpeed * Time.deltaTime;
Vector3 rotatedMovement = transform.TransformDirection(movement);
// Combine movement and gravity
characterController.Move(rotatedMovement + velocity * Time.deltaTime);
// Player rotation (looking around)
float mouseX = Input.GetAxisRaw("Mouse X") * mouseSensitivity;
float mouseY = Input.GetAxisRaw("Mouse Y") * mouseSensitivity;
// Rotate around the Y-axis (left and right)
transform.Rotate(Vector3.up * mouseX * rotationSpeed * Time.deltaTime);
// Rotate around the X-axis (up and down) with clamping
verticalRotation -= mouseY * rotationSpeed * Time.deltaTime;
verticalRotation = Mathf.Clamp(verticalRotation, -maxLookDownAngle, maxLookUpAngle);
cam.transform.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);
// Apply gravity
if (characterController.isGrounded)
{
velocity.y = -0.5f; // Reset vertical velocity when grounded
// Jumping
if (Input.GetKeyDown(KeyCode.Space))
{
velocity.y = Mathf.Sqrt(jumpForce * 2f * gravity);
}
}
else
{
velocity.y -= gravity * Time.deltaTime;
}
}
if (characterController.isGrounded)
{
velocity.y = -0.5f; // Reset vertical velocity when grounded
// Jumping
if (Input.GetKeyDown(KeyCode.Space) && IsLocalPlayer)
{
velocity.y = Mathf.Sqrt(jumpForce * 2f * gravity);
}
}
else
{
velocity.y -= gravity * Time.deltaTime;
}
}
IEnumerator Shoot()
{
gunAnim.SetTrigger("Shoot");
yield return new WaitForSeconds(0.2f);
audioSource.Play();
if (Physics.Raycast(transform.position, transform.forward, out RaycastHit hitInfo, 10))
{
hitInfo.transform.GetComponent<PlayerController>().TakeDamage(damage);
}
attackCooldown = true;
yield return new WaitForSeconds(0.2f);
attackCooldown = false;
}
void TakeDamage(int damage)
{
health.Value -= damage;
if (health.Value <= 0)
{
Destroy(gameObject);
}
}
}