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 /
  • Help Room /
avatar image
0
Question by Der_Kevin · Nov 12, 2015 at 08:01 AM · spawninginttarget

Set Number of Targets based on int Number of Players

Hey! So i got this Camera Script:

 using UnityEngine;
 using System.Collections;
 
 //namespace Complete
 //{
     public class CameraControl : MonoBehaviour
     {
         public float m_DampTime = 0.2f;                 // Approximate time for the camera to refocus.
         public float m_ScreenEdgeBuffer = 4f;           // Space between the top/bottom most target and the screen edge.
         public float m_MinSize = 6.5f;                  // The smallest orthographic size the camera can be.
 
         public Camera m_Camera;                        // Used for referencing the camera.
         public float m_ZoomSpeed;                      // Reference speed for the smooth damping of the orthographic size.
         private Vector3 m_MoveVelocity;                 // Reference velocity for the smooth damping of the position.
         private Vector3 m_DesiredPosition;              // The position the camera is moving towards.
 
         public bool Player1Joined;                        //check which player has Joined
         public bool Player2Joined;
         public bool Player3Joined;
         public bool Player4Joined;
 
         public int numberOfTargets;                        //number of Players joined (1-4)
         public Transform[] m_Targets;                     // All the targets the camera needs to encompass.
             
         public GameObject[] m_Spawnpoints;                //All available Spawnpoints (4)
 
 
 
         private void Awake ()
         {
             m_Camera = GetComponentInChildren<Camera> ();
         }
 
 
         private void FixedUpdate ()
         {
             // Move the camera towards a desired position.
             Move ();
 
             // Change the size of the camera based.
             Zoom ();
         }
 
         private void SetCameraTargets()
         {
             // Create a collection of transforms the same size as the number of tanks.
             Transform[] targets = new Transform[m_Spawnpoints.Length];
             
             // For each of these transforms...
             for (int i = 0; i < targets.Length; i++)
             {
                 // ... set it to the appropriate tank transform.
                 targets[i] = m_Spawnpoints[i].transform;
             }
             
             // These are the targets the camera should follow.
             //m_CameraControl.m_Targets = targets;
             m_Targets = targets;
         }
 
 
         private void Move ()
         {
             // Find the average position of the targets.
             FindAveragePosition ();
 
             // Smoothly transition to that position.
             transform.position = Vector3.SmoothDamp(transform.position, m_DesiredPosition, ref m_MoveVelocity, m_DampTime);
         }
 
 
         private void FindAveragePosition ()
         {
             Vector3 averagePos = new Vector3 ();
             int numTargets = 0;
 
             // Go through all the targets and add their positions together.
             for (int i = 0; i < m_Targets.Length; i++)
             {
                 // If the target isn't active, go on to the next one.
                 if (!m_Targets[i].gameObject.activeSelf)
                     continue;
 
                 // Add to the average and increment the number of targets in the average.
                 averagePos += m_Targets[i].position;
                 numTargets++;
             }
 
             // If there are targets divide the sum of the positions by the number of them to find the average.
             if (numTargets > 0)
                 averagePos /= numTargets;
 
             // Keep the same y value.
             averagePos.y = transform.position.y;
 
             // The desired position is the average position;
             m_DesiredPosition = averagePos;
         }
 
 
         private void Zoom ()
         {
             // Find the required size based on the desired position and smoothly transition to that size.
             float requiredSize = FindRequiredSize();
             m_Camera.orthographicSize = Mathf.SmoothDamp (m_Camera.orthographicSize, requiredSize, ref m_ZoomSpeed, m_DampTime);
         }
 
 
         private float FindRequiredSize ()
         {
             // Find the position the camera rig is moving towards in its local space.
             Vector3 desiredLocalPos = transform.InverseTransformPoint(m_DesiredPosition);
 
             // Start the camera's size calculation at zero.
             float size = 0f;
 
             // Go through all the targets...
             for (int i = 0; i < m_Targets.Length; i++)
             {
                 // ... and if they aren't active continue on to the next target.
                 if (!m_Targets[i].gameObject.activeSelf)
                     continue;
 
                 // Otherwise, find the position of the target in the camera's local space.
                 Vector3 targetLocalPos = transform.InverseTransformPoint(m_Targets[i].position);
 
                 // Find the position of the target from the desired position of the camera's local space.
                 Vector3 desiredPosToTarget = targetLocalPos - desiredLocalPos;
 
                 // Choose the largest out of the current size and the distance of the tank 'up' or 'down' from the camera.
                 size = Mathf.Max(size, Mathf.Abs(desiredPosToTarget.y));
 
                 // Choose the largest out of the current size and the calculated size based on the tank being to the left or right of the camera.
                 size = Mathf.Max(size, Mathf.Abs(desiredPosToTarget.x) / m_Camera.aspect);
             }
 
             // Add the edge buffer to the size.
             size += m_ScreenEdgeBuffer;
 
             // Make sure the camera's size isn't below the minimum.
             size = Mathf.Max (size, m_MinSize);
 
             return size;
         }
 
 
         public void SetStartPositionAndSize ()
         {
             // Find the desired position.
             FindAveragePosition ();
 
             // Set the camera's position to the desired position without damping.
             transform.position = m_DesiredPosition;
 
             // Find and set the required size of the camera.
             m_Camera.orthographicSize = FindRequiredSize ();
         }
     }
 //}

So, the "public int numberOfTargets" and "public bool PlayerXJoined" already gets filled correct based on how much and which players joined (max 4). what i want to do now is, that the "public Transform[] m_Targets" gets filled correct like this: alt text (the Targets fields are the important ones) how can i do this?

targets.png (99.6 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Duckyyy · Nov 12, 2015 at 04:27 PM

I would have something like this.

 int targetCount = 0;
     //Check amount of players
     for (int i = 0; i < players.Length; i++) {
            if (player.hasJoined)
                     targetCount ++;
     } 
     
     //Spawn Targets
     for (int i = 0; i < targetCount; i++) {
            //Spawn Target
     } 
 
 
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 Der_Kevin · Nov 12, 2015 at 05:25 PM 0
Share

thanks! but somehow i am having problems to apply this to the code above :/

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

32 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

Related Questions

Client Spawning Player Objects 0 Answers

Spawning and Collecting an Enemy in a 2D game 0 Answers

Coroutine for spawning prefab 0 Answers

player spawning somewhere distance in the field? 0 Answers

Button Script 0 Answers


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