- Home /
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);
}
}
Is i the index you are currently using (suggest you make it a bit more descriptive than that later!)?
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.
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;
}
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!