- Home /
Help me please with Unity Multiplayer
How to move players when they are spawned over network. This code working when is one player on the stage, but when more one player connect moving is crazy. One player moving another, camera is attached to wrong player and players are not synced. I tried game on two computers. On one computer players move, but on another computer players just stand. Thank you in advance!!!
This is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class Moving : NetworkBehaviour {
public Camera cam;
public CharacterController controller;
public float speedH = 2.0f;
public float speedV = 2.0f;
public float yaw = 0.0f;
public float pitch = 0.0f;
public float movementSpeed=1.5f;
public Rigidbody rb;
public NetworkView nview;
void Start()
{
nview = this.gameObject.GetComponent<NetworkView>();
controller = this.gameObject.GetComponent<CharacterController>();
rb = this.gameObject.GetComponent<Rigidbody>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.lockState = CursorLockMode.None;
}
void Update()
{
if(!isLocalPlayer)
{
return;
}
if(!nview.isMine)
{
cam.gameObject.SetActive(false);
}
if (nview.isMine)
{
this.controller.Move(new Vector3(0, -1, 0) * 2 * Time.deltaTime);
Screen.lockCursor = true;
yaw += speedH * Input.GetAxis("Mouse X");
this.transform.localEulerAngles = new Vector3(0, yaw, 0);
if (Input.GetKey(KeyCode.W))
{
this.controller.Move(new Vector3(cam.transform.forward.x, 0, cam.transform.forward.z) * 2 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
this.controller.Move(new Vector3(cam.transform.forward.x, 0, cam.transform.forward.z) * (-2) * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
this.controller.Move(new Vector3(cam.transform.right.x, 0, cam.transform.right.z) * (-2) * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
this.controller.Move(new Vector3(cam.transform.right.x, 0, cam.transform.right.z) * 2 * Time.deltaTime);
}
}
}
}
$$anonymous$$aybe someone more familiar with the networking system will chime in, but NetworkView is deprecated. NetworkIdentity is the currently supported network identification system.
Do you have a NetworkIdentity Component on that object, and is the LocalPlayerOnly
box checked?
Answer by Chik3r · Feb 26, 2018 at 11:23 PM
For players to move on every computer add a 'NetworkTransform' Component to the player (More info: https://docs.unity3d.com/Manual/class-NetworkTransform.html). For the camera to follow the local player try this:
In your moving script add:
public override void OnStartLocalPlayer()
{
base.OnStartLocalPlayer();
gameObject.name = "Local";
}
And to the Camera script that follows the player add this:
void Start()
{
player = GameObject.Find("Local");
}
// Your follow code..
Your answer
Follow this Question
Related Questions
Check when a player finished spawning [Netcode For GameObjects!] 2 Answers
Unity networking tutorial? 6 Answers
Networking Question: Spawning selectively / on only one client. 1 Answer
How to make UNITY multiplayer with two HTC VIVE in the same real room? 0 Answers
Adding children to player during runtime in a multiplayer game with NetworkTransformChild Component 1 Answer