- Home /
How do I check multiple gameObjects transform positions?
I've been trying to make a shooting game where a load of boxes get knocked off of a pedestal, and a target comes down to shoot after wards. This is what I have so far.
var cubeTag;
var heightToCheck : float = 0.9f;
var target:Rigidbody;
var cubeArray = new Array ();
cubeArray.length = 18;
cubeArray.Push(GameObject.FindWithTag("cubeTag"));
cubeArray.Push(GameObject.FindWithTag("cubeTag1"));
cubeArray.Push(GameObject.FindWithTag("cubeTag2"));
cubeArray.Push(GameObject.FindWithTag("cubeTag3"));
cubeArray.Push(GameObject.FindWithTag("cubeTag4"));
cubeArray.Push(GameObject.FindWithTag("cubeTag5"));
cubeArray.Push(GameObject.FindWithTag("cubeTag6"));
cubeArray.Push(GameObject.FindWithTag("cubeTag7"));
cubeArray.Push(GameObject.FindWithTag("cubeTag8"));
cubeArray.Push(GameObject.FindWithTag("cubeTag9"));
cubeArray.Push(GameObject.FindWithTag("cubeTag10"));
cubeArray.Push(GameObject.FindWithTag("cubeTag11"));
cubeArray.Push(GameObject.FindWithTag("cubeTag12"));
cubeArray.Push(GameObject.FindWithTag("cubeTag13"));
cubeArray.Push(GameObject.FindWithTag("cubeTag14"));
cubeArray.Push(GameObject.FindWithTag("cubeTag15"));
cubeArray.Push(GameObject.FindWithTag("cubeTag16"));
cubeArray.Push(GameObject.FindWithTag("cubeTag17"));
for (var i = 0; i<cubeArray.length; i++)
{
var currentElementBeingTested = cubeArray[i];
//do something with currentElementBeingTested
Debug.Log("cube array y position = "+currentElementBeingTested.Transform.transform.position.y);
}
function CheckForCubeHeight(){ //This function is to check to see if the cube has fallen off of the pedestal.
var cubeToCheck = GameObject.FindWithTag("cubeTag2");
//var cubeToCheck = cubeArray[0];
//Debug.Log("reading cube as "+cubeToCheck);
//Debug.Log("cubeToCheck "+cubeToCheck.transform.position.y +"> Height is "+heightToCheck);
if(cubeToCheck.transform.position.y < heightToCheck){
Debug.Log("cube on floor. Reading y as "+cubeToCheck.transform.position.y);
///target.velocity = transform.TransformDirection (Vector3(0,1,0));
}
}
function Update () {
CheckForCubeHeight();
}
Answer by Owen-Reynolds · Oct 29, 2012 at 01:55 PM
Not sure what the question is (do you want to check when all boxes are on the floor? What if some come to rest on other boxes?) But, you can simplify what you have, to make it easier to think about the rest.
Tags are often used to group several objects together. For example, you might have 50 cubes (walls... ) but only those 18 are the special falling boxes. So, you can make a single "fallBox" tag for all 18 of them. Imagine doing that from the start - you just make one, tag it, then copy and the correct tag is automatically on the new ones.
The purpose of the tag is whenever you want to say "cubes that count as falling boxes," you can use the tag. This can replace those 18 Add lines (and will work no matter how many boxes you have):
cubeArray = GameObject.FindObjectsWithTag("fallBox"); // note the S in objects
Thanks, yeah I had a pretty strong feeling I was using tags wrong.
And sorry if the question wasn't very clear. What i want to do is check when all the boxes are on the floor (or more specifically, when they have fallen off of the pedestal they stand on). The boxes are pretty small compared to the height of the pedestal so I don't think them staking on top of each other will be an issue.
What I'm struggling with though, is a way to code this so that the program checks for when all of the cubes are on the floor.
Answer by Trace_Irving · Oct 30, 2012 at 04:52 PM
Thanks, yeah I had a pretty strong feeling I was using tags wrong.
And sorry if the question wasn't very clear. What i want to do is check when all the boxes are on the floor (or more specifically, when they have fallen off of the pedestal they stand on). The boxes are pretty small compared to the height of the pedestal so I don't think them staking on top of each other will be an issue.
What I'm struggling with though, is a way to code this so that the program checks for when all of the cubes are on the floor.
Answer by Owen-Reynolds · Oct 30, 2012 at 10:56 PM
Arrays are a general topic. Unity arrays work pretty much the same as regular ones, and you can read about them lots of places. Putting together the parts you have:
// count how many are too high. Done when none are too high
var offFloor=0;
for (var i = 0; i<cubeArray.length; i++) {
if(cubeArray[i].transform.position.y > heightToCheck){
offFloor +=1;
}
}
if(offFloor==0) { they are all down }
But this won't check they are at rest -- they could be bouncing back up.