I have 2 Scripts:
and GameObject Player: for control network i have GameObject Scripts: PlayerScript code:
using 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();
}
}
[RPC]
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)
{
Debug.Log("Player " + " connected from " + netPlayer.ipAddress + ":" + netPlayer.port);
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 isn't called and player are spawned twice.. If I'am trying to go to other players positions they start flashing, otherwise they are not visible.. Anybody can help me please? A lot of thanks and sorry for my English :))
You must observe the script, not the transform.
Drag the scrip component to the observed slot. Then it will trigger.