Hello guys I am new to unity, hope I will manage to explain myself properly…
So I have simple game with simple network system but the movement is laggy even in the same computer
so this is what i got:
movment:
input = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));
if (GetComponent<Rigidbody> ().velocity.magnitude < maxSpaad) {
GetComponent<Rigidbody> ().AddRelativeForce (input * moveSpeed);
}
Well yeah nothing more to show . Just want to make that players will see my cube more smoothly.
If you need more information please tell me and i will add.
That piece of code is not enough for us to help you, you should have use of [Command], [ClientCallback] and [SyncVar]
anyway, here is a simple script that syncs position
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using UnityStandardAssets.Characters.FirstPerson;
public class PlayerNetwork : NetworkBehaviour
{
[SyncVar] private Vector3 SyncedPosition;
[SerializeField] Transform PlayerTransform;
[SerializeField] float PlayerSyncRate = 15f;
void FixedUpdate()
{
SendPosition();
ReceivePosition();
}
[Command]
void CmdSendPosition(Vector3 Position)
{
SyncedPosition = Position;
}
[ClientCallback]
void SendPosition()
{
if (isLocalPlayer)
{
CmdSendPosition(transform.position);
}
}
void ReceivePosition()
{
if (!isLocalPlayer)
{
transform.position = Vector3.Lerp(transform.position, SyncedPosition, Time.deltaTime * PlayerSyncRate);
}
}
}
1 Like
You don’t actually have to send the command to set the position if you have set the position as a SyncVar.
I have a “Logic” script like this:
[SyncVar]
private Vector2 movement;
public void setMovement(Vector2 movement) {
this.movement = movement;
}
void FixedUpdate() {
rigidbody.MovePosition(rigidbody.position + movement * Time.deltaTime);
}
And a Control script like this:
void Update() {
if (!isLocalPlayer)
return;
Vector2 movement = Vector2.zero;
if (Input.GetKey(KeyCode.W)
movement.y += 1;
if (Input.GetKey(KeyCode.S)
movement.y -= 1;
paddleLogic.setMovement(movement * speed);
}
(A lot of stuff omitted)
And that seems to work pretty well as a minimalistic movement synchronization example.
5vStudios:
That piece of code is not enough for us to help you, you should have use of [Command], [ClientCallback] and [SyncVar]
anyway, here is a simple script that syncs position
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using UnityStandardAssets.Characters.FirstPerson;
public class PlayerNetwork : NetworkBehaviour
{
[SyncVar] private Vector3 SyncedPosition;
[SerializeField] Transform PlayerTransform;
[SerializeField] float PlayerSyncRate = 15f;
void FixedUpdate()
{
SendPosition();
ReceivePosition();
}
[Command]
void CmdSendPosition(Vector3 Position)
{
SyncedPosition = Position;
}
[ClientCallback]
void SendPosition()
{
if (isLocalPlayer)
{
CmdSendPosition(transform.position);
}
}
void ReceivePosition()
{
if (!isLocalPlayer)
{
transform.position = Vector3.Lerp(transform.position, SyncedPosition, Time.deltaTime * PlayerSyncRate);
}
}
}
Well I tryed it but it does not working well. iput it in the player object.
I notice that he always return false in isLocalPlayer
This is how I define a player:
Network.Instantiate (playerPrefs[numbersOfPlayers],
list[numbersOfPlayers].transform.position,
Quaternion.Euler(new Vector3(0,270,0)),0);
does your player object have a ‘NetworkIdentity’ component attached ?
Yes it is automatic attach to him when i added the script.
Maybe I need to spawn the player differently?
class MyManager : NetworkManager
{
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
GameObject player = (GameObject)Instantiate(playerPrefab, Vector3.Zero, Quaternion.Identity);
player.GetComponent<Player>().color = Color.Red;
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
}
For instance Player you can use this:
Firts you need regist the player connection in the host, you need call like this:
ClientScene.AddPlayer(0);
then, this function will be called when player registration has been completed:
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
When this function is called is the “green light” to instantiate the player, you can do it directly from the same function like this:
/// <summary>
/// Call when add a player to the host
/// </summary>
/// <param name="conn"></param>
/// <param name="playerControllerId"></param>
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
GameObject p;
Transform startPosition = GetStartPosition();
p = (GameObject)UnityEngine.Object.Instantiate(m_playerPrefab, startPosition.position, startPosition.rotation);
//Register the current player created
NetworkServer.AddPlayerForConnection(conn, p, playerControllerId);
// base.OnServerAddPlayer(conn, playerControllerId);
}
Note: to prevent the player is instantiated twice, be sure to uncheck “Auto Create Player” in your NetworkManager.
LovattoStudio:
For instance Player you can use this:
Firts you need regist the player connection in the host, you need call like this:
ClientScene.AddPlayer(0);
then, this function will be called when player registration has been completed:
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
When this function is called is the “green light” to instantiate the player, you can do it directly from the same function like this:
/// <summary>
/// Call when add a player to the host
/// </summary>
/// <param name="conn"></param>
/// <param name="playerControllerId"></param>
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
GameObject p;
Transform startPosition = GetStartPosition();
p = (GameObject)UnityEngine.Object.Instantiate(m_playerPrefab, startPosition.position, startPosition.rotation);
//Register the current player created
NetworkServer.AddPlayerForConnection(conn, p, playerControllerId);
// base.OnServerAddPlayer(conn, playerControllerId);
}
Note: to prevent the player is instantiated twice, be sure to uncheck “Auto Create Player” in your NetworkManager.
Ok so this is what i did:
-I inherit NetworkManager.
-Added the NetworkManager script to my player object.
-Disable “Auto Create Player”.
-Did ClientScene.AddPlayer(0);
When i wanted to spawn a new player.
-Spawn the player:
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
print("spawn");
int numbersOfPlayers=Network.connections.Length ;
GameObject p;
p = (GameObject)UnityEngine.Object.Instantiate(playerPrefs[numbersOfPlayers],
list[numbersOfPlayers].transform.position,
Quaternion.Euler(new Vector3(0,270,0)));
NetworkServer.AddPlayerForConnection(conn, p, playerControllerId);
}
but the problem is that he doesn’t print “spawn”, this function doesnt even called.
Decoder46:
Ok so this is what i did:
-I inherit NetworkManager.
-Added the NetworkManager script to my player object.
-Disable “Auto Create Player”.
-Did ClientScene.AddPlayer(0);
When i wanted to spawn a new player.
-Spawn the player:
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
print("spawn");
int numbersOfPlayers=Network.connections.Length ;
GameObject p;
p = (GameObject)UnityEngine.Object.Instantiate(playerPrefs[numbersOfPlayers],
list[numbersOfPlayers].transform.position,
Quaternion.Euler(new Vector3(0,270,0)));
NetworkServer.AddPlayerForConnection(conn, p, playerControllerId);
}
but the problem is that he doesn’t print “spawn”, this function doesnt even called.
the script is a class inherited from NetworkBehaviour?
I mean you use like this: public class scriptName : NetworkBehaviour{
no, I use it like this: public class NetworkManager : NetworkManager {
Ok so i figure the problem, now he is throwing me an error:
I initialize the server and then I call AddPlayer().
This is what i am doing:
public void StartServer(){
bool useNat = !Network.HavePublicAddress();
MasterServer.ClearHostList ();
Network.InitializeServer(10, port,useNat);
MasterServer.RegisterHost(GameName,lobbyName,"a game");
GetComponent<NetworkManeger>().SpawnPlayer();
ClientScene.AddPlayer(0);
}
Have you found a solution for your last problem? I am having the same message.