Multiplayer Camera Follow. Please Help Im so Close!!
Problem:
I start two clients, one is a host/client and the other is just a client. When the game starts the two players spawn in. The camera then locks on to the second client player (nonhost) and displays on both clients. I have spent 18 hours on this and have not figured it out. I have setup everything else correctly. Each character moves independently, but the camera will just not split.
Goal:
I start two clients, one is a host/client and the other is just a client. When the game starts the two players spawn in. Each client has a separate camera.
Layout:
One camera object in scene tagged as "MainCamera" starting as "active" along with the camera component "enabled."
Bellow is the script I have attached to my camera and the script I have attached to my player prefab.
Camera Script:
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
public class CameraControl : MonoBehaviour
{
private const float Y_ANGLE_MIN = 0.0f;
private const float Y_ANGLE_MAX = 50.0f;
public Transform camTransform;
public float distance = 5.0f;
public Transform lookAt;
private float currentX = 0.0f;
private float currentY = 45.0f;
private float sensitivityX = 4.0f;
private float sensitivityY = 1.0f;
private void Start()
{
camTransform = transform;
}
private void Update()
{
currentX += Input.GetAxis("Mouse X");
currentY += Input.GetAxis("Mouse Y");
currentY = Mathf.Clamp(currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
}
private void LateUpdate()
{
Vector3 dir = new Vector3(0, 0, -distance);
Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
camTransform.position = lookAt.transform.position + rotation * dir;
camTransform.LookAt(lookAt.transform.position);
}
}
Player Prefab Script:
using UnityEngine.Networking; using UnityStandardAssets.Characters.ThirdPerson; using UnityEngine; using System.Collections;
public class LocalPlayerControl : NetworkBehaviour {
private GameObject cameraObject;
private void Start()
{
cameraObject = GameObject.FindGameObjectWithTag("MainCamera");
if (isLocalPlayer)
GetComponent<ThirdPersonUserControl>().enabled = true;
GetComponent<ThirdPersonCharacter>().enabled = true;
GetComponent<Animator>().enabled = true;
GetComponent<CapsuleCollider>().enabled = true;
GetComponent<AudioSource>().enabled = true;
GetComponent<NetworkAnimator>().enabled = true;
GetComponent<NetworkTransform>().enabled = true;
cameraObject.GetComponent<CameraControl>().enabled = true;
cameraObject.GetComponent<CameraControl>().lookAt = GameObject.FindGameObjectWithTag("Player").transform;
cameraObject.GetComponent<Camera>().enabled = true;
// cameraObject.GetComponent<CameraControl>().camTransform = transform;
}
}
Your answer
Follow this Question
Related Questions
Trying to send command for object without authority 0 Answers
Giving authority to clients 0 Answers
HELP Network spawn player (host or client) in same position 0 Answers
Remote clients can no longer connect with new modem / router 0 Answers
Spawning GameObject on network, locally change it only on the client that spawned it. 0 Answers