Making my camera follow player in MultiPlayer
Hi I want each player to have their own pov as all multiplayer games do, and have it follow around the player. I have a camera script tied to my camera, with a target of player object. I have a player prefab which i assign to the camera.
This is my camera script
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class CameraFollow : NetworkBehaviour {
public Transform target;
public float m_speed = 0.1f;
Camera mycam;
// Use this for initialization
void Start () {
mycam = GetComponent<Camera>();
}
// Update is called once per frame
void Update () {
if (!isLocalPlayer)
{
mycam.enabled = false;
}
else
{
mycam.enabled = true;
}
mycam.orthographicSize = (Screen.height / 100f) / 2f;
if (target) {
transform.position = Vector3.Lerp(transform.position, target.position, m_speed) + new Vector3(0, 0, -10);
}
}
}
I dont believe the camera needs to be a networked object unless you intend to view or control it from another client.
If you are using the networking to only use isLocalPlayer, consider finding the camera from your player ins$$anonymous$$d of finding the player from the camera. If it is not a networked object there wont be multiple cameras.
Dont forget, you can inject information with GetComponent as well as retrieve it.
Answer by Jah0 · Nov 09, 2016 at 08:30 AM
This is how I got it workin:
Script attached to my Main Camera:
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform playerTransform;
public int depth = -20;
// Update is called once per frame
void Update()
{
if(playerTransform != null)
{
transform.position = playerTransform.position + new Vector3(0,10,depth);
}
}
public void setTarget(Transform target)
{
playerTransform = target;
}
}
And I set the target in my PlayerController script:
public override void OnStartLocalPlayer()
{
Camera.main.GetComponent<CameraFollow>().setTarget(gameObject.transform);
}
Hope this helps!
Thanks UDN this worked for me and I implemented it in under 2 $$anonymous$$utes. Well Happy.
Thanks indeed. I have been trying different ways but which didn't work till I got this.
awesome... thank you. exactly what was bugging me for some hours now :)
Answer by domapalb · Dec 09, 2018 at 10:44 PM
Edited to fix problem pointed out by @ystetsyk
I know this is a little old, but I found a very simple way to get it to work, Just put this in the PlayerController script:
private Camera myCamera; // Copy of the main camera, attached to the player
public override void OnStartLocalPlayer()
{
myCamera = Instantiate(Camera.main);
myCamera.transform.rotation = transform.rotation;
myCamera.transform.position = transform.position + new Vector3(offsetX, offsetY, offsetZ);
myCamera.transform.SetParent(transform);
}
void Update()
{
// Do update stuff, including moving the player
// ...
Camera.main.CopyFrom(myCamera);
}
First, make a copy of the main camera and position it where you want it for the player, then make it a child of the player. If you were to make the camera a child of the player prefab, then syncing the players would cause the problem of everyone seeing the same camera. Here the camera is not made a part of the prefab so it isn't synced, instead, it is now a child of the Client's player instance.
In Update(), the main camera is now following our player's camera, so that when the player stops the session, only the camera attached is destroyed, not the main camera. They can then join a new game, and it will again just make a camera for the main camera to follow.
Now you can simply manipulate the myCamera camera however you want.
Thanks simple solution but I found problem here: when you disconnect from the server your player is destroyed as well as your camera.
Answer by rak1n · Mar 19, 2016 at 07:30 PM
so I added camera object to player and changed use my player prefab. but I seem to only have one camera in scene, when I join a game it only follows one player.
just delete the other camera: if (!isLocalPlayer) { mycam.enabled = false; } else { mycam.enabled = true; }