Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MrDragon64 · Feb 26, 2018 at 10:27 PM · objectlisttag

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;
 }



}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Positive7 · Feb 26, 2018 at 10:36 PM 0
Share

In your player script Awake or Start add them to the list. Something like :

   private void Start()
         {
             Camera.main.GetComponent<camerafollow>().targets.Add(transform);
         }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrDragon64 · Feb 28, 2018 at 06:21 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

81 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Count >= then object tag becomes active 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges