- Home /
Why is playerControllerId = -1
I thought Local Player has playerControllerId = 0
- Why am I displaying -1
Answer by maturski · Aug 16, 2018 at 10:33 AM
Line 94 -- Just debugging and trying to find out why the camera isn't setting the target as the latest player to spawn
using System;
using UnityEngine;
using UnityEngine.Networking;
namespace UnityStandardAssets.Cameras
{
public abstract class AbstractTargetFollower : NetworkBehaviour
{
public enum UpdateType // The available methods of updating are:
{
FixedUpdate, // Update in FixedUpdate (for tracking rigidbodies).
LateUpdate, // Update in LateUpdate. (for tracking objects that are moved in Update)
ManualUpdate, // user must call to update camera
}
[SerializeField] protected Transform m_Target; // The target object to follow
[SerializeField] private bool m_AutoTargetPlayer = true; // Whether the rig should automatically target the player.
[SerializeField] private UpdateType m_UpdateType; // stores the selected update type
protected Rigidbody targetRigidbody;
protected virtual void Start()
{
// if auto targeting is used, find the object tagged "Player"
// any class inheriting from this should call base.Start() to perform this action!
if (m_AutoTargetPlayer)
{
FindAndTargetPlayer();
}
if (m_Target == null) return;
targetRigidbody = m_Target.GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
// we update from here if updatetype is set to Fixed, or in auto mode,
// if the target has a rigidbody, and isn't kinematic.
if (m_AutoTargetPlayer && (m_Target == null || !m_Target.gameObject.activeSelf))
{
FindAndTargetPlayer();
}
if (m_UpdateType == UpdateType.FixedUpdate)
{
FollowTarget(Time.deltaTime);
}
}
private void LateUpdate()
{
// we update from here if updatetype is set to Late, or in auto mode,
// if the target does not have a rigidbody, or - does have a rigidbody but is set to kinematic.
if (m_AutoTargetPlayer && (m_Target == null || !m_Target.gameObject.activeSelf))
{
FindAndTargetPlayer();
}
if (m_UpdateType == UpdateType.LateUpdate)
{
FollowTarget(Time.deltaTime);
}
}
public void ManualUpdate()
{
// we update from here if updatetype is set to Late, or in auto mode,
// if the target does not have a rigidbody, or - does have a rigidbody but is set to kinematic.
if (m_AutoTargetPlayer && (m_Target == null || !m_Target.gameObject.activeSelf))
{
FindAndTargetPlayer();
}
if (m_UpdateType == UpdateType.ManualUpdate)
{
FollowTarget(Time.deltaTime);
}
}
protected abstract void FollowTarget(float deltaTime);
public void FindAndTargetPlayer()
{
if (this.isLocalPlayer) {
Debug.Log("playerControllerId = " + playerControllerId);
}
var targetObj = GameObject.FindGameObjectWithTag("Player");
if (targetObj)
{
//SetTarget(targetObj.transform);
SetTarget(targetObj.transform);
}
}
public virtual void SetTarget(Transform newTransform)
{
m_Target = newTransform;
}
public Transform Target
{
get { return m_Target; }
}
}
}
Answer by PhoenixFeng · Sep 24, 2018 at 11:18 AM
If a client didn't add any player, the NetworkBehaviour.playerControllerId
is -1 as default .(Yes, the default value of playerControllerId
is -1, not 0. You can try not adding a player when you connect to the server and print(playerControllerId)
to check this.)
If you have ever used ClientScene.AddPlayer(short playerControllerId)
to spawn a new player for the client, the playerControllerId
shouldn't be -1.
How did you spawn your Player? Or have you ever removed the player? Or you just didn't add one?
Your answer
![](https://koobas.hobune.stream/wayback/20220612172907im_/https://answers.unity.com/themes/thub/images/avi.jpg)