- Home /
How can i find a player prefab and add them to a list??????
So i have this game that uses player prefabs and also uses a camera that needs a list of all players in the game, but in a transform list. so i need a way to find all objects with the tag "player", then add them to the cameras list of players. Note the camera is used to keep all players within the view of the camera. "video" https://www.youtube.com/watch?v=aLpixrPvlB8∈dex=1&list=LLbeJJb1EIb7Im_KjdzT_6ag
my code**
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class camerafollow : MonoBehaviour {
public List<Transform> targets;
public Vector3 offset;
public float smoothTime = .5f;
public float minZoom = 40f;
public float maxZoom = 10f;
public float zoomLimiter = 50f;
private int numOfTargets;
private Vector3 velocity;
private Camera cam;
void Start()
{
cam = GetComponent<Camera> ();
}
void Awake()
{
}
void LateUpdate ()
{
if (targets.Count == 0)
return;
Move ();
Zoom ();
}
void Zoom()
{
float newZoom = Mathf.Lerp (maxZoom, minZoom, GetGreatestDistance () / zoomLimiter);
cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, newZoom, Time.deltaTime);
}
void Move()
{
Vector3 centerPoint = GetCenterPoint ();
Vector3 newPosition = centerPoint + offset;
transform.position = Vector3.SmoothDamp(transform.position, newPosition, ref velocity, smoothTime);
}
float GetGreatestDistance()
{
var bounds = new Bounds (targets [0].position, Vector3.zero);
for (int i = 0; i < targets.Count; i++) {
bounds.Encapsulate (targets [i].position);
}
return bounds.size.x;
}
Vector3 GetCenterPoint()
{
if (targets.Count == 1) {
return targets [0].position;
}
var bounds = new Bounds (targets [0].position, Vector3.zero);
for (int i = 0; i < targets.Count; i++) {
bounds.Encapsulate (targets [i].position);
}
return bounds.center;
}
}
In your player script Awake or Start add them to the list. Something like :
private void Start()
{
Camera.main.GetComponent<camerafollow>().targets.Add(transform);
}
Answer by seandolan · Feb 27, 2018 at 05:00 AM
GameObject.FindGameObjectsWithTag("Player") will find all the objects with the player tag. The Unity manual entry for it has all the code you need.
https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html
// Instantiates respawnPrefab at the location
// of all game objects tagged "Respawn".
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public GameObject[] players;
void Start()
{
players = GameObject.FindGameObjectsWithTag("Player");
float minX = 0.0f;
float maxX = 0.0f;
float minY = 0.0f;
float maxY = 0.0f;
foreach (GameObject player in players)
{
// Then something like this:
if(player.transform.position.x < minX){ minX = player.transform.position.x; }
if(player.transform.position.x > maxX){ maxX = player.transform.position.x; }
if(player.transform.position.y < minY){ minY = player.transform.position.y; }
if(player.transform.position.y > maxY){ maxY = player.transform.position.y; }
}
}
// Set your camera to cover minX, minY and maxX, maxY
}
thank you :D this helps but now i have to find the transform of all of the game objects so that the camera can follow them all
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Destroy all objects with tag "enemy"? 2 Answers
FindGameObjectsWithTag check gameObject disabled? 1 Answer
JS to C# List type problem 1 Answer