Fixing Ped.Instance & Ped.IsLocalPlayer

This commit is contained in:
in0finite 2019-04-24 23:02:39 +02:00
parent b5cfb80212
commit 80c0507f0b
3 changed files with 19 additions and 11 deletions

View file

@ -115,12 +115,14 @@ namespace SanAndreasUnity.Behaviours
#endregion Properties
public static Ped Instance { get ; private set ; }
/// <summary>Ped who is controlled by local player.</summary>
public static Ped Instance { get { return Net.Player.Local != null ? Net.Player.Local.OwnedPed : null; } }
/// <summary>Position of player instance.</summary>
public static Vector3 InstancePos { get { return Instance.transform.position; } }
public bool IsLocalPlayer { get; private set; }
/// <summary>Is this ped controlled by local player ?</summary>
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<CharacterController>();
m_weaponHolder = GetComponent<WeaponHolder> ();

View file

@ -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);
}

View file

@ -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<Player> s_allPlayers = new List<Player>();
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; } }
/// <summary>Local player.</summary>
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()
{