How to make camera follow player with Photon fusion?

I currently have a script for the camera to follow players around.

using UnityEngine;
using Photon.Pun;

public class PlayerFollowCam : MonoBehaviour
{
    public Transform target;
    public Vector3 offset;
    [Range(1, 10)]
    public float smoothingfactor;

    private PhotonView view;

    public void Start()
    {
    }

    private void FixedUpdate()
    {
        if (!target) return;
        if(gameObject.activeSelf) { SmoothCameraPanning(); }
    }

    void SmoothCameraPanning()
    {
        Vector3 CameraTarget = target.position + offset;
        Vector3 CameraPosition = Vector3.Lerp(transform.position, CameraTarget, smoothingfactor * Time.fixedDeltaTime);
        transform.position = CameraPosition;
    }
}

It’s currently tied to the main camera.
8459564--1123064--upload_2022-9-23_16-11-11.png
Currently, player objects are instantiated by the networkrunnerhandler. It clones a player prefab. I obviously want the camera to only follow your player on screen.


When I add the cloned players transform to the script, it does not work at all.

Thanks for reading.

This appears to be a 2-dimentional game. Instead of Vector3, use Vector2.

This is a bad idea as it will set the camera’s Z position to 0, which will make the target object not visible at all if it is also at z == 0. Even 2D games are 3D in Unity, and the Z axis still matters.

Anyway I don’t really understand this question:

  • You’re asking about photon fusion, which is a networking framework, which isn’t really relevant to the question since the camera should generally not care about networking bits

  • You asked about Fusion but you’re actually using PUN :eyes:

  • “it does not work at all” what does this mean exactly? Have you inspected the hierarchy and the inspectors of the relevant objects at runtime after spawning the player? Is the offset set up correctly on the camera? Is the camera referring to the correct player object? Have you added any Debug.Log statements or used the debugger to step through and make sure your code is running?

You need to just follow basic debugging steps here.

Oh, I didn’t know that. I am a novice at 2d, but since I am good at 3d, it occurred to me that you should use Vector 2. Sorry about that.

  1. The camera needs to account for which player is local, no? It should only follow the local player, which is obviously unique to each player in the game. Is it possible to set this up without regard to the networking
  2. That’s a good catch, I experimented with PUN before, but I’m using fusion now. This was an old script. I’ve updated it now
  3. The script isn’t behaving as expected. It is acting as if I just dropped a default maincamera into the scene. It is not following the player around