- Home /
Networking Camera not attaching to instantiated object
Hello, I am in the process of trying to convert my camera system from single player to multiplayer. It works great if I connect one user to the server, but once I add in anymore users, the camera seems to attach itself to the first user's version of the game object. Individual movements are controlling their respected characters and they still rotate and move around properly, just the camera is always attached to the first user connected.
This code is from my TP_Camera script.
public static void UseExistingOrCreateNewMainCamera()
{
GameObject tempCamera;
GameObject targetLookAt;
TP_Camera myCamera;
if(Camera.mainCamera != null)
{
tempCamera = Camera.mainCamera.gameObject;
}
else
{
tempCamera = new GameObject("Main Camera");
tempCamera.AddComponent ("Camera");
tempCamera.tag = "MainCamera";
}
tempCamera.AddComponent("TP_Camera");
myCamera = tempCamera.GetComponent ("TP_Camera") as TP_Camera;
targetLookAt = GameObject.Find("targetLookAt") as GameObject;
myCamera.TargetLookAt = targetLookAt.transform;
}
Here is where it is called from the TP_Controller script.
void Awake()
{
if(networkView.isMine == true)
{
CharacterController = GetComponent ("CharacterController") as CharacterController;
Instance = this;
TP_Camera.UseExistingOrCreateNewMainCamera();
}
else
enabled = false;
I am fairly certain that it is occurring because every instance is looking for the first game object "targetLookAt" rather than the child of its instance, but everything I've tried either leads to more problems or just doesn't work.
I did get the network camera to follow the proper instances when I created a simple new camera and had it attach to a transform through transform.FindChild() instead of a game object, but trying to do that within the UseExistingOrCreateNewMainCamera function didn't attach the camera to anything at all.
The myCamera.TargetLookAt = transformHereInsteadOfGameObject; line either didn't attach itself to the transform or transformHereInsteadOfGameObject = transform.FindChild("targetLookAt"); didn't work.
Any tips on the best way to solve this? Thank you.
Answer by Jakeo Spikez · Apr 23, 2013 at 07:19 AM
I figured it out, if anyone else runs into the same problem, here's what I did.
It was indeed because myCamera.TargetLookAt = targetLookAt.transform; was grabbing the first player connected's targetLookAt game object.
My TP_Camera script is attached to my camera and my TP_Controller script is attached to my player prefab. I had to find the child in TP_Controller then bring that into the UseExistingOrCreateNewMainCamera function as a parameter. Setting the value of the transform myCamera.TargetLookAt to a transform that was attached to the child "targetLookAt" of my character properly followed the respective instance of players.
From my TP_Controller script
void Awake()
{
if(networkView.isMine == true)
{
Transform targetLookAtTransform = transform.FindChild ("targetLookAt");
CharacterController = GetComponent ("CharacterController") as CharacterController;
Instance = this;
TP_Camera.UseExistingOrCreateNewMainCamera(targetLookAtTransform);
}
else
enabled = false;
From my TP_Camera script
public static void UseExistingOrCreateNewMainCamera(Transform targetLookAtTransform)
{
GameObject tempCamera;
TP_Camera myCamera;
if(Camera.mainCamera != null)
{
tempCamera = Camera.mainCamera.gameObject;
}
else
{
tempCamera = new GameObject("Main Camera");
tempCamera.AddComponent ("Camera");
tempCamera.tag = "MainCamera";
}
tempCamera.AddComponent("TP_Camera");
myCamera = tempCamera.GetComponent ("TP_Camera") as TP_Camera;
myCamera.TargetLookAt = targetLookAtTransform;
}
Answer by Master Tanker · Aug 06, 2013 at 12:13 AM
Hi, im having the same problem that you, im trying to get working your code but im having errors, did you added more Networview component to something else?
May i see your complete code?
what components did you added to your character prefab? im working over "worker demo" modifiying ThirdPersonNetwork.cs and adding a photonview component.
There is some way to contact you Jakeo Spikez?
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Simple Camera look script (MULTIPLAYER) 0 Answers
Trying to follow tutorial to set up Multiplayer camera follow on UNET... and it goes wrong. 0 Answers
How to trigger camera capture on each player in Unity Networking? 0 Answers
camera layer issue, multiplayer. 1 Answer