X
X
XiNull2020-01-15 00:49:28
C++ / C#
XiNull, 2020-01-15 00:49:28

How to pass the dynamic prefab of the main player to another script (Photon Pun)?

Hello.
There is an object (Game Manager) with a script on it (GameManager.cs).
GameManager.cs spawns a player with a prefab:

using Photon.Pun;
using Photon.Realtime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviourPunCallbacks
{
    public GameObject PlayerPrefab;

    void Start()
    {
        Vector3 pos = new Vector3(Random.Range(-3f, 3f), Random.Range(-3f, 3f));
        PhotonNetwork.Instantiate(PlayerPrefab.name, pos, Quaternion.identity);
    }
...
}

There is a camera (Main Camera), on it the script CameraController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour
{

    public GameObject PlayerPrefab;

    [SerializeField]
    private float speed = 2.0F;

    public Vector3 offset;

    [SerializeField]
    public Transform target;

    private void Awake()
    {
        //if (!target) target = FindObjectOfType<Тут надо найти копию префаба/объекта игрока, который заспавнился, которым будем управлять>().transform;
    }

    private void Update()
    {
        Vector3 position = target.position + offset;
        position.z = -10.0F;
        transform.position = Vector3.Lerp(transform.position, position, speed * Time.deltaTime);
    }

}

You need to somehow pass the dynamic prefab to the script that is on the camera (PlayerPrefab / target), so that the camera is above the player who controls the character.
How to do it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question