- Home /
Top-down multiplayer camera follow
So I am currently working on a top down shooter game, and am able to get the camera to follow a single player top down character. But Im not sure how to set up the camera so that i follow the player. Note that I cant just make the camera a child of the player object as it will cause the character to spin out of control. So how would I be able to make the characters have a camera follow them from above like Dead Frontier AND be able to have each player have their own camera?
Just set the camera's transform.position to the player's position, with a y offset of course so it hovers above him or her, in Update() on a script that's attached to the camera.
By doing that, how would I go about giving each player their own camera? I dont want them sharing a camera. I actually tried to just lock the rotation on the y axis of the camera but it didnt do much. The camera still tried to spin around.
I don't know where your problem is. $$anonymous$$ake as many cameras as you want, and have each use a different player transform to follow.
Answer by nickostan · Apr 02, 2016 at 03:20 PM
Create a parent "PlayerGOs" gameobject.
Under the gameobject, create your player and a camera.
Do NOT parent the camera to the player object, they should be separate, but following the "PlayerGOs" transform.
Create a script on the camera to have the identical position as the player, but add a y-offest of however much you'd like (10-20 units or so).
Something such as:
void cameraFollow()
{
float charPosX = transform.position.x;
float charPosZ = transform.position.z;
float cameraOffset = 18.0f;
viewCamera.transform.position = new Vector3(charPosX, cameraOffset, charPosZ);
}
This script would be placed on the Player gameobject, and reference the camera (doing it publically is likely the easiest). You would also need to call this function every frame in Update or LateUpdate.
void Update (){
cameraFollow();
}
To make sure each player has their own camera in a multiplayer setting, this is a little more complicated, but you'll basically instantiate this PlayerGOs object as the 'player prefab' in your network manager. After that, each script should check if it belongs to the local player, and if not, delete or disable. Such as:
void Update () {
if (!isLocalPlayer)
{
gameObject.SetActive(false); //can probably be done more efficiently so not being called redundantly every frame
return;
}
That's how I do it at least, and it works well with no CPU overhead for me.
Cheers. Vranvs
I follow this and I can't move the player. What components does the "PlayerGOs" object need? Do I have to migrate player's script to PlayerGOs's script? And also, do I need to add NetworkIdentity and NetworkTransform to a camera?