i am working on a multiplayer game and i guess i am just not fully grasping it. i have some code but just cant get things to work. can anyone please look at it and help me identify whats wrong and how it works. i am using netcode for game objects. my thought process for what im trying to do right now is to get input from the client then send it to the server the server does the thing and tells the client to the thing also. this code isnt exactly what i would want but is the result of me just trying lots of things to try and get it.
sorry if its hard to read
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
public class playermove : NetworkBehaviour
{
public Transform playerBody;
public Rigidbody rb;
public float jumpForce = 1f;
private Vector3 moveDirection;
public float moveForce = 4f;
public float lookSpeedX = 2.0f; // Horizontal mouse sensitivity
public float lookSpeedY = 2.0f; // Vertical mouse sensitivity
public float upperLimit = 80f; // Upper limit for vertical look
public float lowerLimit = -80f;
public Camera playerCamera;
private float xRotation = 0f;
private Vector3 direction = Vector3.zero;
void Start()
{
if (rb == null)
{
rb = GetComponent<Rigidbody>();
}
if (IsLocalPlayer)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
else
{
playerCamera.gameObject.SetActive(false); // Disable the camera for other players
}
}
void Update()
{
if (Input.GetKey(KeyCode.Escape))
{
playerCamera.gameObject.SetActive(true); // Enable the camera for the local player
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
playerl();
}
void playerm()
{
if (IsLocalPlayer)
{
direction = Vector3.zero;
// Handle jumping
if (Input.GetKey(KeyCode.Space) && IsGrounded())
{
// Call the server-side function to apply the jump
RequestJumpServerRpc();
}
// Handle movement (WASD keys)
if (Input.GetKey(KeyCode.W)) direction += transform.forward * moveForce; // Move forward
if (Input.GetKey(KeyCode.A)) direction += transform.right * -moveForce; // Move left
if (Input.GetKey(KeyCode.S)) direction += transform.forward * -moveForce; // Move backward
if (Input.GetKey(KeyCode.D)) direction += transform.right * moveForce; // Move right
direction = direction.normalized;
if (direction != Vector3.zero)
{
playermoveServerRpc(direction);
}
else
{
// No movement input, stop the player's movement locally
playerstopServerRpc(); // Keep the y velocity (for jumping/falling)
}
}
}
private bool IsGrounded()
{
return Physics.Raycast(transform.position, Vector3.down, 1.1f);
}
// Jumping logic (ServerRPC and ClientRPC for synchronized jumping)
[ServerRpc]
void RequestJumpServerRpc()
{
JumpClientRpc();
}
[ClientRpc]
void JumpClientRpc()
{
if (IsLocalPlayer)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
[ServerRpc]
void playermoveServerRpc(Vector3 direction)
{
if (IsLocalPlayer)
{
playermoveClientRpc(direction);
}
}
[ClientRpc]
void playermoveClientRpc(Vector3 direction)
{
rb.AddForce(direction * moveForce, ForceMode.Force);
}
[ServerRpc]
void playerstopServerRpc()
{
if (IsLocalPlayer)
{
playerstopClientRpc();
}
}
[ClientRpc]
void playerstopClientRpc()
{
rb.velocity = new Vector3(0, rb.velocity.y, 0);
}
// Handle player camera and mouse input for looking around
void playerl()
{
if (IsLocalPlayer)
{
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
// Rotate the camera horizontally based on the mouseX input
transform.Rotate(Vector3.up * mouseX * lookSpeedX);
// Rotate the camera vertically based on the mouseY input, but clamp the angle to prevent flipping
xRotation -= mouseY * lookSpeedY;
xRotation = Mathf.Clamp(xRotation, lowerLimit, upperLimit);
// Apply the vertical rotation to the camera
Camera.main.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
}
}
}