19.06.2014, 05:24
|
#9
|
Мастер
Регистрация: 24.06.2009
Адрес: Набережные Челны
Сообщений: 930
Написано 292 полезных сообщений (для 504 пользователей)
|
Ответ: Очень нужна ваша помощь
в общем проще ответить как прикреплять к скрипту респавнутый объект
вот коды чего сейчас изучаю:
using UnityEngine; using System.Collections;
public class NetworkManager : MonoBehaviour {
public GameObject playerPrefab;
private const string typeName = "UniqueGameName"; private const string gameName = "RoomName";
private void StartServer() { Network.InitializeServer(4, 25000, !Network.HavePublicAddress()); MasterServer.RegisterHost(typeName, gameName); }
private HostData[] hostList;
private void RefreshHostList() { MasterServer.RequestHostList(typeName); }
void OnMasterServerEvent(MasterServerEvent msEvent) { if (msEvent == MasterServerEvent.HostListReceived) hostList = MasterServer.PollHostList(); }
private void JoinServer(HostData hostData) { Network.Connect(hostData); }
void OnGUI() { if (!Network.isClient && !Network.isServer) { if (GUI.Button(new Rect(100, 100, 250, 100), "Start Server")) StartServer();
if (GUI.Button(new Rect(100, 250, 250, 100), "Refresh Hosts")) RefreshHostList();
if (hostList != null) { for (int i = 0; i < hostList.Length; i++) { if (GUI.Button(new Rect(400, 100 + (110 * i), 300, 100), hostList[i].gameName)) JoinServer(hostList[i]); } } } }
void OnServerInitialized() { SpawnPlayer(); }
void OnConnectedToServer() { SpawnPlayer(); }
private void SpawnPlayer() { Network.Instantiate(playerPrefab, new Vector3(0f, 5f, 0f), Quaternion.identity, 0); }
}
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
public float speed = 10f;
void Start() { if (!networkView.isMine) networkView.observed=this; }
void InputMovement() { if (Input.GetKey(KeyCode.W)) rigidbody.MovePosition(rigidbody.position + Vector3.forward * speed * Time.deltaTime); if (Input.GetKey(KeyCode.S)) rigidbody.MovePosition(rigidbody.position - Vector3.forward * speed * Time.deltaTime); if (Input.GetKey(KeyCode.D)) rigidbody.MovePosition(rigidbody.position + Vector3.right * speed * Time.deltaTime); if (Input.GetKey(KeyCode.A)) rigidbody.MovePosition(rigidbody.position - Vector3.right * speed * Time.deltaTime); }
private float lastSynchronizationTime = 0f; private float syncDelay = 0f; private float syncTime = 0f; private Vector3 syncStartPosition = Vector3.zero; private Vector3 syncEndPosition = Vector3.zero;
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) { Vector3 syncPosition = Vector3.zero; Vector3 syncVelocity = Vector3.zero; if (stream.isWriting) { syncPosition = rigidbody.position; stream.Serialize(ref syncPosition); syncVelocity = rigidbody.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 = rigidbody.position; } }
void Update() { if (networkView.isMine) { InputMovement(); } else { SyncedMovement(); } }
private void SyncedMovement() { syncTime += Time.deltaTime; rigidbody.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay); }
}
или надо б\у камеру удалять, а у спавн-объекта пересоздавать и закреплять?
|
(Offline)
|
|