Position Network Camera to Instanced Object?

This is so stupid I can’t even believe I’m asking this but I’ve been stuck for the past 24 hours and I give up trying to figure this out on my own. I hate asking questions like this because they’ve got to be covered a million times already, but trust me when I say I’ve looked at them all.

I have created a small network test game consisting of two players. I can successfully control each player in their respective games. However, I’m trying to get a FPS camera hooked up to follow each player Prefab. Also, I’m spawning these players at runtime, so it’s not like I can hook things up in the inspector.

As far as I can tell, the best way to do this is NOT by using multiple cameras. Instead, use one camera and change the position of it if it belongs to the client (in other words, the view isn’t being updated over the network, so it’s independent).

I’ve seen ways to do this involving behaviors, but I’ve also seen a lot that aren’t using them either. The farthest I’ve gotten with behaviors is where the opposite camera follows the player… really weird.

However, I’ve also seen ways to do this by updating the camera’s position to the player’s position every update call. I just can’t get this working!

I have a spawn function in my pauseMenu, where I have the player start the server, that spawns the player on the network. When it does so, I’m trying to position the main camera to this object’s position. One of the ways I’ve tried to do this is by using SendMessage to send the player objects position over to the camera script, where it can that update a variable and update it’s own position every update. However this isn’t working. Apparently SendMessage isn’t working because the function it’s calling doesn’t show my Debug line.

I’ve also tried manipulating the camera inside the pauseMenu script by using Camera.main or GameObject.Find (and other variations of find). Still no dice.

So what the hell? What gives? What’s the best way to do this? Behaviors or what? I’d provide code I’ve tried, but frankly it’s a mess from trying so many different things.

PS: I’m using C#, but I can read javascript just fine as well.

What a humbling question… bah.

Got it working. This is my entire cameraScript. Note again that my playerPrefab is spawned at runtime. This requires the playerPrefab to have the “Player” tag.

using UnityEngine;

using System.Collections;

public class cameraScript : MonoBehaviour {

private GameObject target;



// Use this for initialization

void Start () {

	

	this.transform.rotation = Quaternion.identity;

}



// Update is called once per frame

void Update () 

{

	target = GameObject.FindWithTag("Player");

	this.transform.position = new Vector3(target.transform.position.x, target.transform.position.y, target.transform.position.z);

	this.transform.rotation = target.transform.rotation;

}

}