- Home /
OnSerializeNetworkView issus in my 2D game
Hello :)
I have 2 Scripts:
NetworkManager
PlayerScript
and GameObject Player:
for control network i have GameObject Scripts:
PlayerScript code:
sing UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
public float speed = 10f;
private float lastSynchronizationTime = 0f;
private float syncDelay = 0f;
private float syncTime = 0f;
private Vector3 syncStartPosition = Vector3.zero;
private Vector3 syncEndPosition = Vector3.zero;
private void InputMovement()
{
if (Input.GetKey(KeyCode.W))
rigidbody2D.MovePosition(rigidbody2D.position + Vector2.up * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.S))
rigidbody2D.MovePosition(rigidbody2D.position - Vector2.up * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.D))
rigidbody2D.MovePosition(rigidbody2D.position + Vector2.right * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.A))
rigidbody2D.MovePosition(rigidbody2D.position - Vector2.right * speed * Time.deltaTime);
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
Debug.Log("OnSerializeNetworkView!!!!!!!!!!!!!!!!!!!!");
Vector3 syncPosition = Vector3.zero;
Vector3 syncVelocity = Vector3.zero;
if (stream.isWriting)
{
syncPosition = rigidbody2D.position;
stream.Serialize(ref syncPosition);
syncPosition = rigidbody2D.velocity;
stream.Serialize(ref syncVelocity);
}
else
{
stream.Serialize(ref syncPosition);
stream.Serialize(ref syncVelocity);
syncTime = 0f;
syncDelay = Time.time - lastSynchronizationTime;
lastSynchronizationTime = Time.time;
syncEndPosition = syncPosition + syncVelocity * syncDelay;
syncStartPosition = rigidbody2D.position;
}
}
void Awake()
{
lastSynchronizationTime = Time.time;
}
void Update()
{
if (networkView.isMine)
{
InputMovement();
}
else
{
SyncedMovement();
}
}
private void SyncedMovement()
{
syncTime += Time.deltaTime;
rigidbody2D.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
Debug.Log(syncStartPosition+ " ####### " +rigidbody2D.position+ " ####### " +syncEndPosition);
}
}
and NetworkManager code:
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour {
public Transform point1;
public Transform point2;
public GameObject player;
// Use this for initialization
void Start () {
if (Network.isServer) {
Network.Instantiate(player,point1.transform.position,Quaternion.identity,0);
}
}
void OnPlayerConnected ( NetworkPlayer netPlayer)
{
networkView.RPC("net_DoSpawn",RPCMode.All);
}
[RPC]
void net_DoSpawn()
{
Network.Instantiate(player,point2.transform.position,Quaternion.identity,0);
}
}
I tried many times to debug id, but no change. OnSerializeNetworkView isnt called and player are spawnet twice.. If Iam traing to go to other players positions they start flashing, otherwise they are not visible.. Anybody can halp me please? A lot of thanks and sorry for my english :))
Answer by AlexTudo · Apr 18, 2015 at 05:07 PM
First thing I notice that you missed is the fact that if you use OnSerializeNetworkView you need to change the 'observed' object of the NetworkView to the script and not the transform as in picture below.
Also, I think you wanted 'syncVelocity' in the second call below.
if (stream.isWriting)
{
syncPosition = rigidbody2D.position;
stream.Serialize(ref syncPosition);
syncPosition = rigidbody2D.velocity; //<--syncVelocity
stream.Serialize(ref syncVelocity);
}
Let me know what happens next after the changes.
Answer by hannah112 · May 18, 2018 at 12:56 PM
2D has discovered a home basically in the easygoing gaming market, which has been regularly disregarded regardless of it being an extensive market. So PC recreations and applications for versatile and tablet will regularly have 2D amusements. Online Shopping deals in pakistan. Particularly if the diversions are by outside the box designers as it is a less expensive generation cost for them.
Answer by stupidwolf99 · Apr 17, 2020 at 09:19 AM
The scope of 2D graphics and animation is still very vast. Online Shopping in Pakistan The requirement of 3d has grown for the last years but 2D still has it's own share of market.,The scope of 2D graphics and animation is still very vast. gaming is still Online Shopping in Pakistan The requirement of 3d has grown for the last years but 2D still has it's own share of market.
Your answer
Follow this Question
Related Questions
RPC or Synchronization ? 1 Answer
Where is a free, helpful tutorial on how to use Unity's networking? 2 Answers
Is server the sender of RPC? 0 Answers
networkView.RPC() inside a ScriptableObject 1 Answer
Which is better: NetworkView or RPC? 1 Answer