- Home /
Multiplayer camera problem
I've been struck, by the apparently infamous multiplayer camera problem. I have read and researched, but I can not find a solution. I have probably spent around 7-9 hours trying to get eh cameras to work. I have a simple multiplayer game, where there are two forklifts that try and flip each other over, but when the second player spawns in the first player uses their camera. I am relatively new to coding in C#, and wanted to use this as a good learning opportunity. Here is the code I have attached to the camera of my prefab as of writing this: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class caselupa : NetworkBehaviour {
// Use this for initialization
void Start () {
if (!gameObject.transform.parent.gameObject.GetComponent<NetworkView>().isMine)
{
gameObject.SetActive(true);
}
}
// Update is called once per frame
void Update () {
if (!gameObject.transform.parent.gameObject.GetComponent<NetworkView>().isMine)
{
gameObject.SetActive(true);
}
}
}
As I said I am relatively new to C#, and so I tried using other peoples' code, and it would not work, so I took what info I could gather, and made that bit of code. The way I understand it, if the network view of the parent object is not owned by the local player then don't use that camera. Any help I could get would be great, It is 9:00 now and I have been working on this problem for the past 3 hours.
edit: about a half hour after posting this I did a little test, I just set the camera to inactive, in the inspector, and made a code that just on start set the camera as active, and every frame checked to see if the camera was active. The thing is nothing happened. So I have one of two conclusions. Either A: I am just so bad at programming that I can't set something as active, or B: the code, for whatever reason, is not affecting my game object.
Answer by lll4louis · Jun 30, 2018 at 08:28 AM
I had this problem before, and soon I find out that you should not put the camera into the Player.
and in this camera, I make two script for it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace S3
{
public class CameraFollow : MonoBehaviour
{
public Transform playerTransform;
public int depth = 0;
// Update is called once per frame
void Update()
{
if (playerTransform != null)
{
transform.position = playerTransform.position + new Vector3(0, 0, depth);
}
}
public void setTarget(Transform target)
{
playerTransform = target;
}
}
}
and the next script
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
namespace S3
{
public class CameraController : NetworkBehaviour
{
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
float z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
transform.Rotate(0, x, 0);
transform.Translate(0, 0, z);
}
}
}
so, it will become a FPS game, hope it works.