- Home /
How to make Camera follow clone player ?,How to make Camera follow cloned object?
My game is set up that when a player joins the game a character will spawn here is that script:
public class SpawnPlayers : MonoBehaviour { public GameObject playerPrefab;
public float minX;
public float minY;
public float maxX;
public float maxY;
private void Start()
{
Vector2 randomPosition = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
PhotonNetwork.Instantiate(playerPrefab.name, randomPosition, Quaternion.identity);
}
}
my camera script works like this: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Camera_fllow : MonoBehaviour { public Transform target; public Vector3 offset;
// Update is called once per frame
void LateUpdate()
{
transform.position = target.position + offset;
}
}
is there any way that i can make my camera's "target" to the character my script spawns?,In my game the characters are created by this script when a player joins the game:
using System.Collections; using System.Collections.Generic; using UnityEngine; using Photon.Pun;
public class SpawnPlayers : MonoBehaviour { public GameObject playerPrefab;
public float minX;
public float minY;
public float maxX;
public float maxY;
private void Start()
{
Vector2 randomPosition = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
PhotonNetwork.Instantiate(playerPrefab.name, randomPosition, Quaternion.identity);
}
}
My camera following script is set up like this: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Camera_fllow : MonoBehaviour { public Transform target; public Vector3 offset;
// Update is called once per frame
void LateUpdate()
{
transform.position = target.position + offset;
}
}
Is there any way I can set "target" to the player the SpawnPlayers script creates ?
Answer by AaronBacon · Nov 30, 2021 at 01:56 PM
Yep, the easiest solution would just be to make a static reference to the Camera flow script, then just have the same line that spawns the player store it in a variable so you can set the target to it, like so:
⠀
Camera Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera_fllow : MonoBehaviour
{
public Transform target;
public Vector3 offset;
public static Camera_fllow mainCam;
// Awake is called when the script instance is being loaded.
void Awake()
{
mainCam = this;
}
// Update is called once per frame
void LateUpdate()
{
transform.position = target.position + offset;
}
}
⠀
And then on the Player Spawn script:
⠀
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class SpawnPlayers : MonoBehaviour
{
public GameObject playerPrefab;
public float minX;
public float minY;
public float maxX;
public float maxY;
private void Start()
{
Vector2 randomPosition = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
GameObject newPlayer = PhotonNetwork.Instantiate(playerPrefab.name, randomPosition, Quaternion.identity);
Camera_fllow.mainCam.target = newPlayer.transform;
}
}