Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 serenefox · Mar 27, 2014 at 04:11 AM · c#cameraarrayintindex

Array index help.

So I have been modifying the Unity 2D demo's "CameraFollow" script and I believe I am so close to getting want I need. My players are put in the array I have setup but I can't seem to step through the array by incrementing the index. I think I just need to somehow link the players(GameObjects) in the array to an int, but how do I do that? That way I can tell it to go to the next player in the array if the current player is not "activeInHierarchy". (Two of three of my players are not active in the scene, and I swap between them so I am trying to get the camera to follow the active player). If anyone can give me some advice or an answer to this I would be very grateful. Here is my modified script of the "CameraFollow":

 using UnityEngine;
 using System.Collections;
 
 public class CameraFollow : MonoBehaviour 
 {
     public float xMargin = 1f;        // Distance in the x axis the player can move before the camera follows.
     public float yMargin = 1f;        // Distance in the y axis the player can move before the camera follows.
     public float xSmooth = 8f;        // How smoothly the camera catches up with it's target movement in the x axis.
     public float ySmooth = 8f;        // How smoothly the camera catches up with it's target movement in the y axis.
     public Vector2 maxXAndY;        // The maximum x and y coordinates the camera can have.
     public Vector2 minXAndY;        // The minimum x and y coordinates the camera can have.
 
     public GameObject player;
     public int i = 0;
     public GameObject[] playerObject = new GameObject[3];        // Reference to the player's transform.
     public Transform players;
 
 
     void Awake ()
     {
         // Setting up the reference.
         playerObject = GameObject.FindGameObjectsWithTag("Player");
         for(i = 0; i < playerObject.Length; i++)
         {
             foreach(GameObject player in playerObject)
             {
 
                 players = player.transform;
             }
         }
     }
 
 
     bool CheckXMargin()
     {
         // Returns true if the distance between the camera and the player in the x axis is greater than the x margin.
         return Mathf.Abs(transform.position.x - players.position.x) > xMargin;
     }
 
 
     bool CheckYMargin()
     {
         // Returns true if the distance between the camera and the player in the y axis is greater than the y margin.
         return Mathf.Abs(transform.position.y - players.position.y) > yMargin;
     }
 
     void Update()
     {
         if(players.gameObject.activeInHierarchy == false)
         {
             //i++;
         }
     }
 
     void FixedUpdate ()
     {
         TrackPlayer();
     }
     
     
     void TrackPlayer ()
     {
         // By default the target x and y coordinates of the camera are it's current x and y coordinates.
         float targetX = transform.position.x;
         float targetY = transform.position.y;
 
         // If the player has moved beyond the x margin...
         if(CheckXMargin())
             // ... the target x coordinate should be a Lerp between the camera's current x position and the player's current x position.
             targetX = Mathf.Lerp(transform.position.x, players.position.x, xSmooth * Time.deltaTime);
 
         // If the player has moved beyond the y margin...
         if(CheckYMargin())
             // ... the target y coordinate should be a Lerp between the camera's current y position and the player's current y position.
             targetY = Mathf.Lerp(transform.position.y, players.position.y, ySmooth * Time.deltaTime);
 
         // The target x and y coordinates should not be larger than the maximum or smaller than the minimum.
         targetX = Mathf.Clamp(targetX, minXAndY.x, maxXAndY.x);
         targetY = Mathf.Clamp(targetY, minXAndY.y, maxXAndY.y);
 
         // Set the camera's position to the target position with the same z component.
         transform.position = new Vector3(targetX, targetY, transform.position.z);
     }
 }

 
Comment
Add comment · Show 2
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 whydoidoit · Mar 27, 2014 at 05:58 AM 0
Share

Is i the index you are currently using (suggest you make it a bit more descriptive than that later!)?

avatar image serenefox · Mar 27, 2014 at 06:11 AM 0
Share

Yes "i" is the index I am using, sorry I have been at this for six hours trying to figure this and other stuff out.

1 Reply

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

Answer by whydoidoit · Mar 27, 2014 at 06:22 AM

So if you want to move to the next available active player then something like this in TrackPlayer at the start.

  var c = 0;
  while(!players.gameObject.activeInHierarchy && c++ < playerObject.Length+1) {
     i = (i + 1) % playerObject.Length;
     players = playerObject[i].transform;
  }
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 serenefox · Mar 27, 2014 at 06:52 AM 0
Share

Thank you so much I would have been here for days without that. I have heard of modulus but never thought I would use or need it. I thought I was getting a good hold of this c# scripting but I guess I still need to keep hitting the books(I have only taken two scripting classes both of which were in JavaScript in unity, and have taught myself c# based what I know and have read). But yeah again thanks for the help I have been like I said sitting here for hours trying to get this(although the first two hours I was trying to figure out the first part). I will have to research a few more things to see how your answer worked because at a glance I don't understand how it some of it fixed everything. But I am tired and am going to bed but thank you!

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

21 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

Related Questions

Indexes behaving weirdly 0 Answers

C# - IndexOutOfRangeException - Array index is out of range 2 Answers

How to set a specific gameobject in an array false when it collides with a trigger. 1 Answer

strange cloning array beviour 1 Answer

Monodevelop is expecting int from my float array 3 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