如何让相机跟随克隆播放器而不是第一次生产?

How to make camera follow to clone player not first production?

我做了一个可以跟随克隆播放器unity的代码。但它只对第一个克隆玩家有效。如果第二次或以后加入房间,它找不到克隆播放器并且不起作用。我该如何解决?感谢您的帮助。


follow.cs:

private void Awake()
    {
        mainCam = this;
    }

    void Start()
    {
        height = Camera.main.orthographicSize;
        width = height * Screen.width / Screen.height;
    }

    void LateUpdate()
    {
        if (player == null)
        {
            player = GameObject.Find("Player(Clone)");
        }
        else
        {
            //transform.position = new Vector3(target.position.x, target.position.y, -10f);

            transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime * speed);

            float lx = size.x * 0.5f - width;
            float clampX = Mathf.Clamp(transform.position.x, -lx + center.x, lx + center.x);

            float ly = size.y * 0.5f - height;
            float clampY = Mathf.Clamp(transform.position.y, -ly + center.y, ly + center.y);

            transform.position = new Vector3(clampX, clampY, -10f);
        }
    }

玩家生成脚本:

public void Spawn()
{
    GameObject PI = PhotonNetwork.Instantiate("Player", Vector3.zero, Quaternion.identity);
    Follow.mainCam.target = PI.transform;
    
    Debug.Log("I'm in the room.");
}

我猜想 player 留在 null,也许当你试图用他的名字 GameObject.Find() 时,那个方法 returns null

也许你可以尝试使用 tag 或使用其他名称?