- Home /
how to find local player in multiplayer game?
so I have trouble finding local player to attach camera to it.
I used following script to make camera follow the player
//attach this script to camera
GameObject Player;
public Vector3 offset;
void Update ()
{
Player = GameObject.FindWithTag("Player");
transform.position = Player.transform.position + offset;
}
notice that find game object is in update method and not in start method because when game starts there is no player in scene and when player spawns in the scene, script couldnt find it because it only looks once. also notice I'm finding player with its tag. so with this script when I hit play and spawn player, camera does follow player.
.
but the problem is script doesnt know which one is local player so it starts following the first player that spawns in the scene. when client player joins the server, camera of client player hovers over host player only and doesnt follow that client player. so I used following script to make camera follow local player only.
//attach this script to camera instead of one above.
GameObject Player;
public Vector3 offset;
void Update ()
{
Player = GameObject.FindWithTag("Player");
if (isLocalPlayer)
{
transform.position = Player.transform.position + offset;
}
else
{
return;
}
}
(FYI because this script is driven by network behavior camera has network identity component attached to it and I have tick marked local player authority). and suddenly camera doesnt follow either of the player, ie host player and client player. that means it couldnt find gameobject with "player" tag on it. or maybe problem is something else I dont know.
what I think is happening with 2nd script is with isLocalPlayer function it is finding camera itself as a local player (because local player authority is tick marked) and not the actual player of the game.
.
so may be this is not how you find local player. so I'm looking for a way to find local player and make my camera follow it. ie host camera follow host player and client camera follow client player. thank you in advance.
sorry, I forgot to mention one thing. Camera is not child of player. and I dont want to make it that way. it is top down racing game so I dont want camera to rotate with player.
Answer by Bubinga_Studios · Aug 01, 2018 at 03:11 PM
What I would do is include the camera and player in the same Player Prefab, if you are trying to make an individual view. Like the picture below. The script I use to make smooth camera movement is a Vector3.Lerp. Here is the simplified code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FixCamToPlayer : MonoBehaviour {
public Transform Player;
public float Speed = 1;
public Transform Pivot;
// Use this for initialization
// Update is called once per frame
void FixedUpdate () {
if(Player != null) {
Pivot.position = Vector3.Lerp(Pivot.position, Player.position, Speed);
}
}
}
A Speed value of 1 will lock it onto the character rather than moving towards player. Using fixed update, it will update every frame, to keep it clean.
If I did not answer your question the way you wanted, let me know! Hope this helps!
hi, thanks for the reply. I tried your method but unfortunately it didnt work. I ran into all sorts of new problems. so here's things I did.
made empty game object, CP
made camera and player child of CP
added network identity component to CP (because network manager only spawns gameobjects that has network identity attached to it.)
attached your script to player with $$anonymous$$or adjustments, as follows
public Transform Player;
public float Speed = 1;
public Transform Pivot;
public Vector3 offset;
void FixedUpdate()
{
if (Player != null)
{
Pivot.position = Vector3.Lerp(Pivot.position, Player.position + offset, Speed);
}
}
offset because camera spawns inside the player.assigned player to player and main camera to pivot
made a if(!isLocalPlayer) script to disable player and camera movement and attached it to CP
when I hit play and host player spawns, everything looks fine.
but when client player joins the scene, client camera does follow client player, but host camera suddenly stops following host player. I couldnt figure out what problem is with this one.
also network transform is attached to player (not to CP) but movement of individual player is not translated.
so basically with your method, client camera does follow client player, but host camera stops following host player after client joins in.
how did it worked for you did you face any problems with this method?
thanks again for your response.
Answer by dontdiedevelop · Aug 02, 2018 at 11:03 AM
//attach to camera
public class CameraSc : NetworkBehaviour
{
GameObject Player;
public Vector3 offset;
public void SetPlayer(Transform newPlayer)
{
Player = newPlayer;
}
void Update ()
{
if(!isLocalPlayer)
return;
if(!Player)
return;
transform.position = Player.transform.position + offset;
}
}
//attach to player
public class PlayerSc : NetworkBehaviour
{
void Start()
{
if(!isLocalPlayer)
return;
if(GameObject.FindObjectOfType<CameraSc>())
GameObject.FindObjectOfType<CameraSc>().SetPlayer(transform);
else
Debug.LogError("Camera Not Found");
}
}
Your answer
Follow this Question
Related Questions
Photon server how to control player traffic in a client network 2 Answers
Creating a client player and a host player in a single device and networking them correspondingly 0 Answers
my multiplayer game second player not active 0 Answers
How Do Camera Follow Mullti Playeres 2 Answers
How to Represent Player Objects in Unity Multiplayer Board Game 0 Answers