Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Unity Networking, connecting to sever problems :( 1 Answer

How to instatiate two players simultaneoulsy before loading level 1 Answer

yield return WWW; not returning 1 Answer

Network Chat not sending messages to other clients 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges