#Multiplayer Basic Setup

1 messages · Page 1 of 1 (latest)

junior turret
#

What Networking solution are you using?

fickle siren
#

i am using netcode

junior turret
#

pc 1 starts hosting; pc 2 joins; pc 1 camera switches to pc 2's camera pov

You can only have one Main Camera in your scene. Either you use one camera and attach it to the local client when they connect. Or you attach the camera to your player prefab and disable it for remote clients.

#

pc 1 moves but nothing of the actions are updated on pc 2 and viece versa

How are you trying to move your players?

fickle siren
#

Which I used in the networkmanager

junior turret
#

I would reccomend ClientNetworkTransform if you're a beginner with NGO.

fickle siren
#

How do I do that? I am very new to NGO and overall networking

junior turret
#

And then you can add it like any other Unity component

fickle siren
#

Thank you. Just a question: how do I disable the camera in the player prefab for remote clients

junior turret
junior turret
#

I'd recommend watching some tutorials if you're brand new to NGO and multiplayer. It can take a bit to wrap your head around the concepts.

fickle siren
#

Also this is my player prefab setup. The player obj has a rigidbody. Do I have to put the Networking Rigidbody on the prefab?

junior turret
#

You can pair NetworkRigidbody with ClientNetworkTransform

fickle siren
#

thanks! I will test it out and see if it works :D

#

it doesent work. like at all

#

also while everyone can walk, even with the fact that nothing updates on the differenet clients, the host cant

junior turret
#

Like what you have set up and what isn't working

#

and probably send some code

fickle siren
# junior turret You'll have to be more specific for me to be able to help

okay: this is my movement script

using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;

public class movementScript : NetworkBehaviour
{
    public Transform orientation;
    private bool grounded;
    public float length;
    public Animator animator;
    public float groundDrag;
    public LayerMask whatisGround;
    public float moveSpeed = 5f;
    Vector3 moveDirection;
    Rigidbody rb;
    float horizontalinput;
    float verticalinput;

    // Start is called before the first frame update
    private void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.freezeRotation = true;
    }

    private void MyInput()
    {
        horizontalinput = Input.GetAxisRaw("Horizontal");
        verticalinput = Input.GetAxisRaw("Vertical");
    }

    // Update is called once per frame
    private void Update()
    {
   

        if (Mathf.Abs(rb.velocity.x) > 1f || Mathf.Abs(rb.velocity.z) > 1f)
        {
            animator.SetBool("isRunning", true);
        }
        else
        {
            animator.SetBool("isRunning", false);
        }
#
        grounded = (Physics.Raycast(transform.position, Vector3.down, length * 0.5f));
        if (grounded)
            rb.drag = groundDrag;
        else
            rb.drag = 0;
        if (Input.GetKeyDown(KeyCode.Space) && grounded)
        {
            animator.SetTrigger("jump");
            rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y + 8f, rb.velocity.z);

            Debug.Log("Jump action executed");
        }
    }

    void FixedUpdate()
    {
        if (!IsLocalPlayer) return; // Only execute this script for the local player

        movePlayer();
    }

    void movePlayer()
    {
        moveDirection = orientation.forward * verticalinput + orientation.right * horizontalinput;

        if (moveDirection != Vector3.zero)
        {
           
            Quaternion targetRotation = Quaternion.LookRotation(moveDirection);
            Quaternion rotationOffset = Quaternion.Euler(0, -90, 0);

            targetRotation *= rotationOffset;
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * moveSpeed);
        }

        if (grounded)
        {
            rb.AddForce(moveDirection.normalized * moveSpeed * 7f, ForceMode.Force);
        }
        else
        {
            rb.AddForce(moveDirection.normalized * moveSpeed * 1f, ForceMode.Force);
        }
    }
}```
#

setup:

#

movement script is attached to the player obj; i have attached the components network object and network transform to the playerprefab itself; i have attached the components network transform network object and network rigidbody to the player obj.

junior turret
#

NetworkTransform or ClientNetworkTransform?

fickle siren
junior turret
#

I would use ClientNetworkTransform. Otherwise your clients have to ask the server to execute your movement code.

fickle siren
#

i just checked and i dont have a clientnetworktransform component

#

ill go install it

junior turret
#

You can add it from package manager or just make the script yourself

#
using Unity.Netcode.Components;
using UnityEngine;

/// <summary>
    /// Used for syncing a transform with client side changes. This includes host. Pure server as owner isn't supported by this. Please use NetworkTransform
    /// for transforms that'll always be owned by the server.
    /// </summary>
    [DisallowMultipleComponent]
    public class ClientNetworkTransform : NetworkTransform
    {
        /// <summary>
        /// Used to determine who can write to this transform. Owner client only.
        /// This imposes state to the server. This is putting trust on your clients. Make sure no security-sensitive features use this transform.
        /// </summary>
        protected override bool OnIsServerAuthoritative()
        {
            return false;
        }
    }
fickle siren
#

ah thanks

junior turret
#

All it does is inherit from NetworkTransform and changes one thing lol

fickle siren
#

I got an error when the other client joined
Spawning NetworkObjects with nested NetworkObjects is only supported for scene objects. Child NetworkObjects will not be spawned over the network!
UnityEngine.Debug:LogError (object)
Unity.Netcode.NetworkSpawnManager:SpawnNetworkObjectLocally (Unity.Netcode.NetworkObject,ulong,bool,bool,ulong,bool) (at Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Spawning/NetworkSpawnManager.cs:625)
Unity.Netcode.NetworkConnectionManager:HandleConnectionApproval (ulong,Unity.Netcode.NetworkManager/ConnectionApprovalResponse) (at Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Connection/NetworkConnectionManager.cs:755)
Unity.Netcode.ConnectionRequestMessage:Handle (Unity.Netcode.NetworkContext&) (at Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Messaging/Messages/ConnectionRequestMessage.cs:156)
Unity.Netcode.NetworkMessageManager:ReceiveMessage<Unity.Netcode.ConnectionRequestMessage> (Unity.Netcode.FastBufferReader,Unity.Netcode.NetworkContext&,Unity.Netcode.NetworkMessageManager) (at Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Messaging/NetworkMessageManager.cs:582)
Unity.Netcode.NetworkMessageManager:HandleMessage (Unity.Netcode.

#

NetworkMessageHeader&,Unity.Netcode.FastBufferReader,ulong,single,int) (at Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Messaging/NetworkMessageManager.cs:446)
Unity.Netcode.NetworkMessageManager:ProcessIncomingMessageQueue () (at Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Messaging/NetworkMessageManager.cs:472)
Unity.Netcode.NetworkManager:NetworkUpdate (Unity.Netcode.NetworkUpdateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Core/NetworkManager.cs:49)
Unity.Netcode.NetworkUpdateLoop:RunNetworkUpdateStage (Unity.Netcode.NetworkUpdateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Core/NetworkUpdateLoop.cs:192)
Unity.Netcode.NetworkUpdateLoop/NetworkEarlyUpdate/<>c:<CreateLoopSystem>b__0_0 () (at Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Core/NetworkUpdateLoop.cs:215)

junior turret
#

You can't have NetworkObjects on children of a parent object that has a NetworkObject

fickle siren
#

ill record whats happening

#

also its a test so it looks shitty

#

and i run the host in the unity editor so i can see the errors

junior turret
#

Nice!

fickle siren
#

the only issue now is that the host cant move and that the script you provided gave me some errors and the cameras are bugged

fickle siren
#

it told me

#

Assets\cameraRotation.cs(28,26): error CS0115: 'cameraRotation.OnNetworkSpawn()': no suitable method found to override

junior turret
#

OnNetworkSpawn needs to be used in a NetworkBehaviour

fickle siren
#

oh

junior turret
#

so that your input doesn't control other clients

fickle siren
fickle siren
#

oh but i didnt at the camerarotation script

#

brb

junior turret
#

and make that a NetworkBehaviour

fickle siren
junior turret
#

Any script can be a NetworkBehaviour as long as the script is on an object with a NetworkBehaviour

fickle siren
#

what

#

huh

#

wdym

#

how do i make something a network behaviour

junior turret
#

Make the script inherit from NetworkBehaviour instead of MonoBehaviour

fickle siren
#

aha

#

thanks

#

i forgot 💀

#

my brain has been rotting from 4 hours

#

of bugs

#

final test

#

it works... kinda

#

tysm for helping me!

junior turret
#

np! Just keep at it

fickle siren
#

gn

#

pfft one thing only and im going to sleep sorry for asking so much i must annoy you but when i put a network animator, and clients join nothing updates like in the begining of the conversation

junior turret
#

Something like OwnerNetworkAnimator?

#

Sorry I haven't used NGO in a long time

fickle siren
fickle siren
junior turret
#

You sure?

#

I'm not sure if it's also sample code like ClientNetworkTransform or if it's built in

junior turret
#
    public class OwnerNetworkAnimator : NetworkAnimator
    {
        protected override bool OnIsServerAuthoritative()
        {
            return false;
        }
    }
#

You're correct

#

It's just like ClientNetworkTransform

fickle siren
#

IT WORKS!!!

#

tysm for helping!!!!

#

good night

#

🫡

junior turret
#

NICE