#Role Based Multiplayer Game

1 messages · Page 1 of 1 (latest)

fickle pine
#

I am trying to make a co-op game in there will be one character and 2 players will control it one will control the mouse and one will control the keyboard movements, i am trying to assign these roles based on client IDs when they join the lobby, I am using netcode for gameobjects, but it’s not working out properly. I am new to multiplayer

If anyone is down to help me let me know pls

spring ore
#

Post your code here so we can see what you are trying. Basically, your 2nd player will need to send it's inputs to the host as an RPC.

fickle pine
#

ahh with RPC let me try that

#

using Unity.Netcode;
using UnityEngine;
public class PlayerController : NetworkBehaviour
{

private CharacterController characterController;
[SerializeField] private float moveSpeed = 10f;

private float rotationSpeed = 10f;
CameraController cameraController;


void Start()
{
    characterController = GetComponent<CharacterController>();
    cameraController = Camera.main.GetComponent<CameraController>();
}

void Update()
{

    if (SharedController.KeyboardPlayer.Value == NetworkManager.Singleton.LocalClientId)
    {
        HandleMovement();
    }


}

void HandleMovement()
{
    float vertical = Input.GetAxisRaw("Vertical");
    MoveServerRpc(vertical);

}

[ServerRpc]
void MoveServerRpc(float vertical)
{

    Vector3 camForward = cameraController.transform.forward;
    camForward.y = 0f;
    camForward.Normalize();

    Vector3 move = moveSpeed * vertical * camForward;
    characterController.SimpleMove(move);

    if (move.magnitude > 0.1f)
    {
        Quaternion targetRotation = Quaternion.LookRotation(camForward);
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
    }
}

}

#

using Unity.Netcode;
using UnityEngine;

public class CameraController : NetworkBehaviour
{

private Transform cameraTransform;

private void Start()
{
    if (Camera.main != null)
    {
        cameraTransform = Camera.main.transform;
    }
    else
    {
        Debug.LogError("No main camera found in the scene!");
    }
}

private void LateUpdate()
{
    if (SharedController.MousePlayer.Value == NetworkManager.Singleton.LocalClientId)
    {
        HandleCamera();
    }
}

void HandleCamera()
{
    HandleCameraServerRpc();
}

[ServerRpc]
void HandleCameraServerRpc()
{
    float mouseX = Input.GetAxis("Mouse X");
    float mouseY = Input.GetAxis("Mouse Y");


    if (cameraTransform == null) return;

    cameraTransform.Rotate(Vector3.up, mouseX, Space.World);
    cameraTransform.Rotate(Vector3.right, -mouseY, Space.Self);
}

}

#

using Unity.Netcode;
using UnityEngine;

public class SharedController : NetworkBehaviour
{

public enum PlayerRole
{
    None,
    Keyboard, 
    Mouse    
}

public static NetworkVariable<ulong> KeyboardPlayer = new NetworkVariable<ulong>(
    ulong.MaxValue, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Server);

public static NetworkVariable<ulong> MousePlayer = new NetworkVariable<ulong>(
    ulong.MaxValue, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Server);

public override void OnNetworkSpawn()
{
    if (IsServer)
    {
        NetworkManager.Singleton.OnClientConnectedCallback += AssignRole;
    }
}


private void AssignRole(ulong clientId)
{
    if (KeyboardPlayer.Value == ulong.MaxValue) 
    {
        KeyboardPlayer.Value = clientId;
        Debug.Log($"Assigned Keyboard role to {clientId}");
    }
    else if (MousePlayer.Value == ulong.MaxValue) 
    {
        MousePlayer.Value = clientId;
        Debug.Log($"Assigned Mouse role to {clientId}");
    }
}

}

#

when i run the game both player can control the camera with mouse but no one can move with keyboard

#

i don't know how to assign the role on the network so i tried the chatgpt

spring ore
#

1st, don't rely on chatgpt. It doesn't know squat about Netcode.
2nd, it's easier to read if you post your !code this way

3rd, you handle camera server RPC need to send the vector 2 from the mouse input int he RPC parameters.

The host client id will always be 0. The first client to connect will always be client id 1.
Having someone remote controlling the camera is going be a bad time. It will cause a lot of motion sickness. But if you must, I would put a network transform on the camera. This will sync the camera to all clients

#

!code

rustic citrusBOT
fickle pine
#

got it

#

i will try that

fickle pine
#

using Unity.Netcode;
using UnityEngine;
public class PlayerController : NetworkBehaviour
{

    private CharacterController characterController;
    [SerializeField] private float moveSpeed = 10f;

    private float rotationSpeed = 10f;
    CameraController cameraController;


    public override void OnNetworkSpawn()
    {
        base.OnNetworkSpawn();

        characterController = GetComponent<CharacterController>();
        cameraController = Camera.main.GetComponent<CameraController>();
    }

    void Update()
    {

        if (NetworkManager.Singleton.LocalClientId == 0)
        {
            Debug.Log("Client ID " + NetworkManager.Singleton.LocalClientId);
            HandleMovement();
        }
        else
        {

            enabled = false;
        }
    }

    void HandleMovement()
    {
        float vertical = Input.GetAxisRaw("Vertical");
        MoveServerRpc(vertical);

    }

    [ServerRpc]
    void MoveServerRpc(float vertical)
    {

        Vector3 camForward = cameraController.transform.forward;
        camForward.y = 0f;
        camForward.Normalize();

        Vector3 move = moveSpeed * vertical * camForward;
        characterController.SimpleMove(move);

        if (move.magnitude > 0.1f)
        {
            Quaternion targetRotation = Quaternion.LookRotation(camForward);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
        }
    }
}


#

i kind of close to the result the issue is here that the first player still be able to controll the camera ,2nd player is not able to move that's and can controll the camera that's what i want that's fine, but 1st player can controll the camera and when i rotate the camera from the 2nd player it's not sync i have aaded the network transform in the camera

fickle pine
#

oh figured it out, now just one issue remaining camera rotation is not sync for the other player

#

i can't figure that out, cause i am using cinemachine camera and main camera has the brain of it what , and i have added the network transform in the main camera, is that right way to do it?

#

or do i need to attach it to the cinemachine's gameobj?

#

what i want is i want to move the player in the direction of where the camera looking like how do i sync that

spring ore
fickle pine
#
using Unity.Cinemachine;
using Unity.Netcode;
using UnityEngine;
public class PlayerController : NetworkBehaviour
{
    private CharacterController characterController;
    CameraController cameraController;
    [SerializeField] private float moveSpeed = 10f;

    private float rotationSpeed = 10f;
    private CinemachineInputAxisController cinemachineInputController;
    Transform secondChild;

    public override void OnNetworkSpawn()
    {
        base.OnNetworkSpawn();

        characterController = GetComponent<CharacterController>();
        cameraController = GetComponent<CameraController>();

        secondChild = transform.GetChild(1);
        cinemachineInputController = secondChild.GetComponent<CinemachineInputAxisController>();

    }

    void Update()
    {

        if (NetworkManager.Singleton.LocalClientId == 0)
        {
            Debug.Log("Client ID " + NetworkManager.Singleton.LocalClientId);


            if (cinemachineInputController != null)
            {
                cinemachineInputController.enabled = false;
                Debug.Log("Disabled CinemachineInputAxisController");
            }

            HandleMovement();
        }
        else
        {
            enabled = false;
        }
    }

    void HandleMovement()
    {
        float vertical = Input.GetAxisRaw("Vertical");
        MoveServerRpc(vertical);

    }

    [ServerRpc]
    void MoveServerRpc(float vertical)
    {

        Vector3 camForward = cameraController.transform.forward;
        Debug.Log("CameForward " + camForward);
        camForward.y = 0f;
        camForward.Normalize();

        Vector3 move = moveSpeed * vertical * camForward;
        characterController.SimpleMove(move);

        if (move.magnitude > 0.1f)
        {
            Quaternion targetRotation = Quaternion.LookRotation(camForward);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
        }
    }
}
#

i am sending the transform.forward here, but it seems in the 1st player camera not moving at all so i think it won't move in the direction of the camera

#

but i do have to sync the camera rotation from the 2nd player so it goes in the direction of camera looking