- Home /
Question by
DylanCaroooo · Nov 18, 2013 at 02:33 AM ·
networkingpositioning
Unity networking thirdperson position not being set for all players
I have a unity multiplayer game and when you spawn for you a firstperson controller is spawned and for every one else a thirdperson model is spawned but the position is not always being set for the other players please help i cannot have more than 2 people with out 1 person not having another person position set. Any help is appreciated.
I have this attached to the player prefab
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UserPlayer : MonoBehaviour {
public Transform FirstPerson;
public WeaponManager FirstPersonCont;
public Transform ThirdPerson;
public Player MyPlayer;
public Vector3 CurPos;
public Quaternion CurRot;
public GameObject RagDoll;
public Player LastShootBy;
public string LastShootByName;
public UserPlayer INSt;
public List<ThirdPersonGuns> ThirdPersonGuns= new List<ThirdPersonGuns>();
void Start () {
if(networkView.isMine)
{
MyPlayer = NetworkManager.GetPlayer(networkView.owner);
MyPlayer.Manager = this;
}
FirstPerson.gameObject.SetActive(false);
ThirdPerson.gameObject.SetActive(false);
DontDestroyOnLoad(gameObject);
}
[RPC]
public void RequestPlayer(string Nameee)
{
networkView.RPC("GiveMyPlayer",RPCMode.OthersBuffered, Nameee);
}
[RPC]
public void GiveMyPlayer(string n)
{
StartCoroutine(GivePlayer(n));
}
IEnumerator GivePlayer(string nn)
{
while(!NetworkManager.HasPlayer(nn))
{
yield return new WaitForEndOfFrame();
}
MyPlayer = NetworkManager.GetPlayer(nn);
MyPlayer.Manager = this;
}
void OnLevelWasLoaded(int level) {
if (level == 0){
Network.Destroy(gameObject);
}
FirstPerson.gameObject.SetActive(false);
ThirdPerson.gameObject.SetActive(false);
MyPlayer.IsAlive = false;
}
void Update () {
ThirdPerson.position = CurPos;
ThirdPerson.rotation = CurRot;
}
[RPC]
public void FindHitter(string name)
{
LastShootBy = NetworkManager.GetPlayer(name);
LastShootByName = name;
}
[RPC]
public void GetKill ()
{
RankManager.Inst.Exp += 100;
RankManager.Inst.Kills += .5f;
}
[RPC]
void Server_TakeDamage(float Damage)
{
networkView.RPC("Client_TakeDamage",RPCMode.Server,Damage);
}
[RPC]
void Client_TakeDamage(float Damage)
{
MyPlayer.Health-=Damage;
Debug.Log(MyPlayer.Health);
if(MyPlayer.Health <= 0)
{
MyPlayer.IsAlive = false;
networkView.RPC("Die",RPCMode.All);
MyPlayer.Health = 0;
Instantiate(RagDoll, ThirdPerson.position,ThirdPerson.rotation);
Instantiate(RagDoll, FirstPerson.position,FirstPerson.rotation);
}
}
[RPC]
void Spawn()
{
MyPlayer.Health = 100;
MyPlayer.IsAlive = true;
if(networkView.isMine)
{
FirstPerson.gameObject.SetActive(true);
ThirdPerson.gameObject.SetActive(false);
}
if(!networkView.isMine){
FirstPerson.gameObject.SetActive(false);
ThirdPerson.gameObject.SetActive(true);
}
}
[RPC]
void Die()
{
MyPlayer.IsAlive = false;
FirstPerson.gameObject.SetActive(false);
ThirdPerson.gameObject.SetActive(false);
LastShootBy.Manager.networkView.RPC("GetKill",LastShootBy.OnlinePlayer);
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info){
if(stream.isWriting){
CurPos = FirstPerson.position;
CurRot = FirstPerson.rotation;
stream.Serialize(ref CurPos);
stream.Serialize(ref CurRot);
}
else{
stream.Serialize(ref CurPos);
stream.Serialize(ref CurRot);
ThirdPerson.position = CurPos;
ThirdPerson.rotation = CurRot;
}
}
public void Server_GetGun(string name)
{
networkView.RPC("Client_GetGun",RPCMode.All, name);
Debug.Log(name);
}
[RPC]
public void Client_GetGun(string name)
{
Debug.Log(name);
foreach(ThirdPersonGuns tpg in ThirdPersonGuns)
{
if(tpg.Name == name)
{
tpg.Obj.SetActive(true);
Debug.Log(tpg + "Is Correct Gun");
}
else{
tpg.Obj.SetActive(false);
Debug.Log(tpg + "Is Not Correct Gun");
}
}
}
}
[System.Serializable]
public class ThirdPersonGuns
{
public string Name;
public GameObject Obj;
}
and this attached to a gameobject (They communicate a bit)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NetworkManager : MonoBehaviour {
public string PlayerName;
public string MatchName;
public static NetworkManager Instance;
public List<Player> PlayerList = new List<Player>();
public Player MyPlayer;
public GameObject SpawnPlayer;
public bool MatchStarted;
public bool MatchLoaded;
public Level CurLevel;
public List<Level> ListOfLevels = new List<Level>();
//public List<Transform> SpawnPoints = new List<Transform>();
//public GameObject[]SpawnPoints;
// Use this for initialization
void Start () {
Instance = this;
DontDestroyOnLoad(gameObject);
}
// Update is called once per frame
void Update () {
PlayerName = PlayerPrefs.GetString("PlayerName");
}
[RPC]
public void StartServer(string ServerName, int MaxPlayers){
Network.InitializeSecurity();
Network.InitializeServer(MaxPlayers,25565,true);
MasterServer.RegisterHost("Tut",ServerName,"");
Debug.Log("started server");
}
void OnPlayerConnected(NetworkPlayer id)
{
foreach(Player pl in PlayerList){
networkView.RPC("GetLevel",id,CurLevel.LoadName,MatchStarted);
networkView.RPC("Client_PlayerJoined",id,MyPlayer.PlayerName,MyPlayer.OnlinePlayer);
}
}
void OnPlayerDisconnected(NetworkPlayer id)
{
networkView.RPC("RemovePlayer",RPCMode.All, id);
Network.RemoveRPCs(id);
Network.Destroy(GetPlayer(id).Manager.gameObject);
}
void OnDisconnectedFromServer(NetworkDisconnection info)
{
PlayerList.Clear();
Application.LoadLevel(0);
}
void OnConnectedToServer ()
{
networkView.RPC("Server_PlayerJoined",RPCMode.Server,PlayerName, Network.player);
}
[RPC]
public void Client_PlayerJoined(string Username,NetworkPlayer id){
Player temp = new Player();
temp.PlayerName = Username;
temp.OnlinePlayer = id;
PlayerList.Add(temp);
if(Network.player == id)
{
MyPlayer = temp;
GameObject LastPlayer = Network.Instantiate(SpawnPlayer, Vector3.zero,Quaternion.identity,0) as GameObject;
LastPlayer.networkView.RPC ("RequestPlayer",RPCMode.AllBuffered,Username);
//temp.Manager = LastPlayer.GetComponent<UserPlayer>();
}
}
void OnServerInitialized()
{
Server_PlayerJoined(PlayerName, Network.player);
}
public static Player GetPlayer(NetworkPlayer id)
{
foreach(Player pl in Instance.PlayerList)
{
if(pl.OnlinePlayer == id)
return pl;
}
return null;
}
[RPC]
public void Server_PlayerJoined(string Username,NetworkPlayer id)
{
networkView.RPC("Client_PlayerJoined",RPCMode.All,Username, id);
}
[RPC]
public void RemovePlayer(NetworkPlayer id)
{
Player temp = new Player();
foreach(Player pl in PlayerList)
{
if(pl.OnlinePlayer == id)
{
temp = pl;
}
if (temp != null)
{
PlayerList.Remove(temp);
}
}
}
[RPC]
public void LoadLevel(string LoadName)
{
Application.LoadLevel(LoadName);
MatchStarted = true;
}
[RPC]
public Level GetLevel(string LoadName, bool IsStarted)
{
if(IsStarted)
LoadLevel(LoadName);
foreach(Level lvl in ListOfLevels)
{
if(LoadName == lvl.LoadName)
{
CurLevel = lvl;
return lvl;
}
}
return null;
}
public static bool HasPlayer(string n){
foreach(Player pl in Instance.PlayerList)
{
if(pl.PlayerName == n)
return true;
Debug.Log(true);
}
return false;
Debug.Log(false);
}
void OnGUI()
{
if(MatchStarted == true && !MyPlayer.IsAlive)
{
if(GUI.Button (new Rect(Screen.width - 60,0,60,20),"Spawn"))
{
MyPlayer.Manager.FirstPersonCont.Spawn();
int SpawnIndex = UnityEngine.Random.Range(0,LevelManager.ins.SpawnPoints.Length - 1);
MyPlayer.Manager.FirstPerson.localPosition = LevelManager.ins.SpawnPoints[SpawnIndex].transform.position;
MyPlayer.Manager.FirstPerson.localRotation = LevelManager.ins.SpawnPoints[SpawnIndex].transform.rotation;
MyPlayer.Manager.networkView.RPC("Spawn",RPCMode.All);
}
}
if(MatchStarted == true)
{
if(GUI.Button (new Rect(Screen.width - 80,20,80,20),"Disconnect"))
{
Application.LoadLevel(0);
Network.Disconnect();
Destroy(gameObject);
}
}
}
public static Player GetPlayer(string id)
{
foreach(Player pl in Instance.PlayerList)
{
if(pl.PlayerName == id)
return pl;
}
return null;
}
}
[System.Serializable]
public class Player{
public string PlayerName;
public NetworkPlayer OnlinePlayer;
public float Health = 100;
public UserPlayer Manager;
public bool IsAlive;
}
[System.Serializable]
public class Level
{
public string LoadName;
public string PlayName;
}
Comment
Your answer
