2

I have this script attached to my camera:

public class CameraFollow : MonoBehaviour {

    public Transform target;

    public float smoothSpeed = 0.125f;
    public Vector3 offset;

    private void Start()
    {
        target = ?        
    }

    private void LateUpdate()
    {
        transform.position = target.position + offset;
    }
}

Since my player is instantiated, I can not drag the prefab to the public target. What can I type in the Start function go set the target as my instantiated player. It has the tag "Avatar".

I am using PhotonNetwork, so I will have several players in a room.

Alp Altunel
  • 3,324
  • 1
  • 26
  • 27
Skram
  • 103
  • 1
  • 2
  • 9
  • It will depend on which player you want to make camera follow. If its the your player and not other peoples character then setting the target to your character should be easy. Instead of calling the target in start function wait for character to spawn. There will be some callback when your character spawns. When it spawns you can assign camera target and start the camera move function. I have not used photon before but did some networking using ulink quite a while ago. – killer_mech Nov 08 '18 at 06:45
  • We waited for character to spawn & used to assign camera to player after it spawns. Also made spectator mode using same principle. Get the desired player id , get its gameobject and assign camera to that gameobject. Think of it this way, Camera does not necessarily has to follow any object but can remain static also or can be randomly assigned to any object in scene if need arises. – killer_mech Nov 08 '18 at 06:47
  • I believe photon should have some ID & relative gameobject ie avatar assigned to each player. You can try to search for it and it should give you your player + other connected players from that list. If not then you should keep a list of players connected and handle the connections. – killer_mech Nov 08 '18 at 06:49
  • You can check on this document i came across for calling https://answers.unity.com/questions/788724/how-to-get-player-ids-from-owners-name-pun-network.html – killer_mech Nov 08 '18 at 06:53
  • Thank you! I will look into the callback from PhotonNetwork. – Skram Nov 08 '18 at 07:33

1 Answers1

2

One way is to modify Start() to:

private void Start()
{
    GameObject[] players = GameObject.FindAllGameObjectsWithTag("Avatar");
    foreach (GameObject player in players)
    {
        if (PhotonView.Get(player).isMine)
        {
            this.target = player.transform;
            break;
        }
    }
}

This with the assumption that your player class inherits from Photon.MonoBehaviour if it isn't already.

PhotonView.Get(player).isMine checks whether the network object is owned by your client, which would therefore be able to differentiate between your player and another person's player. Once you have found the reference to your own player, you can assign it as the camera's target.

Swift
  • 3,250
  • 1
  • 19
  • 35