From 80c0507f0b9dc34f9b8bce672389d0be9fc07734 Mon Sep 17 00:00:00 2001 From: in0finite Date: Wed, 24 Apr 2019 23:02:39 +0200 Subject: [PATCH] Fixing Ped.Instance & Ped.IsLocalPlayer --- Assets/Scripts/Behaviours/Ped/Ped.cs | 11 ++++------- Assets/Scripts/Behaviours/SpawnManager.cs | 4 ++-- Assets/Scripts/Networking/Player.cs | 15 +++++++++++++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Assets/Scripts/Behaviours/Ped/Ped.cs b/Assets/Scripts/Behaviours/Ped/Ped.cs index 0b41b12c..a370c250 100644 --- a/Assets/Scripts/Behaviours/Ped/Ped.cs +++ b/Assets/Scripts/Behaviours/Ped/Ped.cs @@ -115,12 +115,14 @@ namespace SanAndreasUnity.Behaviours #endregion Properties - public static Ped Instance { get ; private set ; } + /// Ped who is controlled by local player. + public static Ped Instance { get { return Net.Player.Local != null ? Net.Player.Local.OwnedPed : null; } } /// Position of player instance. public static Vector3 InstancePos { get { return Instance.transform.position; } } - public bool IsLocalPlayer { get; private set; } + /// Is this ped controlled by local player ? + public bool IsLocalPlayer { get { return this == Ped.Instance; } } @@ -128,11 +130,6 @@ namespace SanAndreasUnity.Behaviours void Awake() { - if (null == Instance) { - Instance = this; - IsLocalPlayer = true; - } - characterController = GetComponent(); m_weaponHolder = GetComponent (); diff --git a/Assets/Scripts/Behaviours/SpawnManager.cs b/Assets/Scripts/Behaviours/SpawnManager.cs index 8dbac2a6..3d6c0730 100644 --- a/Assets/Scripts/Behaviours/SpawnManager.cs +++ b/Assets/Scripts/Behaviours/SpawnManager.cs @@ -87,12 +87,12 @@ namespace SanAndreasUnity.Behaviours { // if player is not spawned, spawn him - if (player.OwnedGameObject != null) + if (player.OwnedPed != null) continue; var spawn = list.RandomElement(); var ped = Ped.SpawnPed(Ped.RandomPedId, spawn.position, spawn.rotation); - player.OwnedGameObject = ped.gameObject; + player.OwnedPed = ped; Debug.LogFormat("Spawned ped for player {0}, net id {1}", player.connectionToClient.address, ped.netId); } diff --git a/Assets/Scripts/Networking/Player.cs b/Assets/Scripts/Networking/Player.cs index 3ee6c619..d5c3e91f 100644 --- a/Assets/Scripts/Networking/Player.cs +++ b/Assets/Scripts/Networking/Player.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using UnityEngine; using Mirror; +using SanAndreasUnity.Behaviours; namespace SanAndreasUnity.Net { @@ -11,8 +12,12 @@ namespace SanAndreasUnity.Net static List s_allPlayers = new List(); public static Player[] AllPlayers { get { return s_allPlayers.ToArray(); } } - [SyncVar] GameObject m_ownedGameObject; - public GameObject OwnedGameObject { get { return m_ownedGameObject; } internal set { m_ownedGameObject = value; } } + /// Local player. + public static Player Local { get; private set; } + + [SyncVar] Ped m_ownedPed; + //public GameObject OwnedGameObject { get { return m_ownedGameObject; } internal set { m_ownedGameObject = value; } } + public Ped OwnedPed { get { return m_ownedPed; } internal set { m_ownedPed = value; } } void OnEnable() @@ -25,6 +30,12 @@ namespace SanAndreasUnity.Net s_allPlayers.Remove(this); } + public override void OnStartLocalPlayer() + { + base.OnStartLocalPlayer(); + Local = this; + } + void Start() {