unity-game-enginemultiplayer

Unity Netcode | Can't dissociate 2 players aim


I'm making a small game for me and my friends. I am using Netcode to make the multiplayer part, but I don't understand why my players always aim at the same point on the screen.

So, what happen is that my player as a top down view, to aim the player as to put is cursor where the character should aim for. But the problem is when one of them rotate they all rotate...

the problem in images: https://imgur.com/a/CnGg28u

Here is my code for the aim part:

    private (bool success, Vector3 position) GetMousePosition()
    {
        /*if (!IsOwner)
            return (success: false, position: Vector3.zero);*/

        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out var hitInfo, Mathf.Infinity, groundLayerMask))
            return (success: true, position: hitInfo.point);
        else
            return (success: false, position: Vector3.zero);
    }

    private void Aim()
    {
        /*if (!IsOwner)
            return;*/

        var (success, position) = GetMousePosition();

        if (success && !player.isDead)
        {
            Vector3 direction = position - transform.position;
            direction.y = 0;
            //Debug.Log($"ID:{OwnerClientId} {position}, {direction}");

            isDebugAimPosition = true;
            aimPosition = position + new Vector3(0, 1.5f, 0);

            transform.forward = direction;
        }
    }

    void Update()
    {
        if (!IsOwner)
            return;

        Aim();
    }

And I got another problem that does not seem to be one... When a client join I got this error:

[Netcode] NetworkPrefab hash was not found! In-Scene placed NetworkObject soft synchronization failure for Hash: 3312273862!

it refers to my player prefab which is acting normal (except for the aim)... My player prefab is in the player prefab's slot of my network manager and in a network prefab list attached to the network manager too. I don't understand this one, but it seems harmless...

Thank for your time and precious help! Have a nice day


Solution

  • try running the client on a second machine

    client connection to host

    on your host run in your terminal/command prompt

    ipconfig
    

    then look for IPv4 Address in the output of that command.

    enter image description here

    in my example the value is 192.168.1.99

    use this value on your client game, to connect to your host using that value instead of localhost or 127.0.0.1

    Help

    to open command prompt.

    press the windows key and the r buttons on your keyboard. then type in cmd into the text box, then press enter on your keyboard

    then type in and run ipconfig and follow the previous steps

    note

    this will only work if both machines are on the same network, either it be wifi or ethernet (lan)