#Multiplayer Basic Setup
1 messages · Page 1 of 1 (latest)
i am using netcode
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?
ah thanks
I created a movement script first for singleplayer then packed it in the player prefab
Which I used in the networkmanager
You need to use a NetworkTransform or ClientNetworkTransform to sync position to other players
I would reccomend ClientNetworkTransform if you're a beginner with NGO.
How do I do that? I am very new to NGO and overall networking
Thank you. Just a question: how do I disable the camera in the player prefab for remote clients
public override void OnNetworkSpawn()
{
if (!IsLocalPlayer)
cameraObj.gameObject.SetActive(false);
}
usually something like that
thanks
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.
https://docs-multiplayer.unity3d.com/netcode/current/about/ and reading this will help a lot
Overview of Unity's Netcode for GameObjects for your multiplayer networking needs.
Also this is my player prefab setup. The player obj has a rigidbody. Do I have to put the Networking Rigidbody on the prefab?
If you have any sort of collisions in your game, I'd recommend it.
You can pair NetworkRigidbody with ClientNetworkTransform
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
You'll have to be more specific for me to be able to help
Like what you have set up and what isn't working
and probably send some code
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.
NetworkTransform or ClientNetworkTransform?
networktransform
I would use ClientNetworkTransform. Otherwise your clients have to ask the server to execute your movement code.
i just checked and i dont have a clientnetworktransform component
ill go install it
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;
}
}
ah thanks
All it does is inherit from NetworkTransform and changes one thing lol
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)
Make sure the NetworkObject is on PlayerPrefab 1 and nothing else.
You can't have NetworkObjects on children of a parent object that has a NetworkObject
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
it worked!!!
Nice!
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
this one
it told me
Assets\cameraRotation.cs(28,26): error CS0115: 'cameraRotation.OnNetworkSpawn()': no suitable method found to override
OnNetworkSpawn needs to be used in a NetworkBehaviour
oh
Also you need to make sure your input/movement code is gated by
if (!IsLocalPlayer) return;
at the top of your functions
so that your input doesn't control other clients
but then how do i reference all the cameraobj
i used it
oh but i didnt at the camerarotation script
brb
You should have some script like PlayerController on your player prefab that stores references to things like the camera
and make that a NetworkBehaviour
oh i can make any object a network behaviour?
Any script can be a NetworkBehaviour as long as the script is on an object with a NetworkBehaviour
The same way you made this one
Make the script inherit from NetworkBehaviour instead of MonoBehaviour
aha
thanks
i forgot 💀
my brain has been rotting from 4 hours
of bugs
final test
it works... kinda
tysm for helping me!
np! Just keep at it
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
is there more than one NetworkAnimator component?
Something like OwnerNetworkAnimator?
Sorry I haven't used NGO in a long time
np
no..?
You sure?
I'm not sure if it's also sample code like ClientNetworkTransform or if it's built in
its not built in
public class OwnerNetworkAnimator : NetworkAnimator
{
protected override bool OnIsServerAuthoritative()
{
return false;
}
}
You're correct
It's just like ClientNetworkTransform
NICE