- Home /
Question by
00george · Dec 15, 2015 at 10:06 PM ·
networkingplayerreferencefindgameobjectswithtag
UNET networking - client side object referencing player object
Hi I have just started getting into multiplayer but I have been having trouble with referencing the player object from a client side object (in this case the camera referencing the player).
I am trying to get the player's transform for my camera follow script but as there can be multiple players due to the game being multiplayer I am not sure how to find the right one using Gameobject.FindGameObjectWithTag
I have come up with a basic solution, however it does not seem like the best way to do it.
Here is my code so far, which does work but I am not sure its the right way of doing it:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class CameraFollowPlayer : MonoBehaviour {
Transform playerTransform;
public Vector3 offset;
GameObject[] players;
void Update () {
if (!playerTransform)
{
players = GameObject.FindGameObjectsWithTag("Player");
for (int i = 0; i < players.Length; i++)
{
if (players[i].GetComponent<NetworkIdentity>().isLocalPlayer == true)
{
playerTransform = players[i].transform;
}
}
}
if (playerTransform)
transform.position = playerTransform.position + offset;
}
}
Comment
How did you solved this? I'm doing what you do right now and it feels wrong.