- Home /
Custom network transform only recognizing new position from the host
Hello!!!
Recently I decided to make my own network transform component to compensate for the lagginess.
Code:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
[NetworkSettings (channel = 0, sendInterval = 0.033f)]
public class NetworkTransformSync : NetworkBehaviour {
public bool syncPosition;
public bool syncRotationY;
[SyncVar]
private Vector3 syncPos;
[SyncVar]
private float syncRotY;
public float lerpRate;
void Update () {
TransmitSync ();
}
void FixedUpdate () {
LerpTransform ();
}
void LerpTransform () {
if (!isLocalPlayer)
{
transform.position = Vector3.Lerp (transform.position, syncPos, lerpRate * Time.deltaTime);
transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.Euler (0, syncRotY, 0), lerpRate * Time.deltaTime);
}
}
[Command]
void CmdSyncTransform () {
if (syncPosition)
{
syncPos = transform.position;
}
if (syncRotationY)
{
syncRotY = transform.rotation.y;
}
}
[ClientCallback]
void TransmitSync () {
if (isLocalPlayer)
{
CmdSyncTransform ();
}
}
}
My only problem is, it only syncs positions to the host. The server does not receive positions and as a result thinks the player is always at 0 0 0. Help?
I've been stuck on this all day, It really would be nice if I could borrow a second pair of eyes on this :/
@meat5000 did you figure anything out because im really stuck on this UNET stuff :/ $$anonymous$$y productivity is limited right now to designing levels every day, and ive been helping others ins$$anonymous$$d of working.
I have another issue too. Other computers cannot connect to my server, and my computer cannot connect to other servers. this is a recurring issue i have had, on and off. If i could have some guidance, that guidance would be much appreciated.
Answer by Soahr · Nov 27, 2015 at 10:13 AM
Hi,
Thanks for the part of code. This help me so much for my dev. Hope this help !
using UnityEngine;
using UnityEngine.Networking;
[NetworkSettings (channel = 0, sendInterval = 0.04f)]
public class NetworkTransform_Custom : NetworkBehaviour {
public float lerpRate;
[SerializeField]
Transform playerPrefab;
[SerializeField]
Transform playerCamera;
[SyncVar]
private Vector3 syncPos;
[SyncVar]
private float syncRotY;
[SyncVar]
private float syncRotX;
void Update ()
{
TransmitSync ();
}
void FixedUpdate ()
{
LerpTransform ();
}
void LerpTransform ()
{
if (!isLocalPlayer)
{
playerPrefab.position = Vector3.Lerp (playerPrefab.position, syncPos, lerpRate * Time.deltaTime);
playerPrefab.rotation = Quaternion.Lerp (playerPrefab.rotation, Quaternion.Euler (0, syncRotY, 0), lerpRate * Time.deltaTime);
playerCamera.rotation = Quaternion.Lerp (playerCamera.rotation, Quaternion.Euler(syncRotX, playerCamera.rotation.eulerAngles.y, 0), lerpRate * Time.deltaTime);
}
}
[Command]
void CmdSyncTransform (Vector3 pos, float rotY, float rotX)
{
syncPos = pos;
syncRotY = rotY;
syncRotX = rotX;
}
[ClientCallback]
void TransmitSync ()
{
if (isLocalPlayer)
{
CmdSyncTransform (playerPrefab.position, playerPrefab.rotation.eulerAngles.y, playerCamera.rotation.eulerAngles.x);
}
}
}
For connection issues, make sure your ports are opened.
Your answer
Follow this Question
Related Questions
How to sync child objects of a transform in multiplayer 3 Answers
How to sync players in multiplayer game 1 Answer
Networking sync enabled state? 1 Answer
[UNET] TRANSFORMATION NOT SYNCING 0 Answers
Sync Var and transform 1 Answer