Hi Y’all,
I’ve finally made time to learn Unity and am having some trouble.
Here’s the code (I’m thinking it’s the last function that I’ve written incorrectly, but I dunno):
private var boundsArray = new Array();
private var textArray = new Array();
private var curBounds = 0;
private var eye : GameObject;
private var t = 0;
function Start() {
eye = GameObject.Find("eye");
makeBounds();
makeTexts();
}
function Update() {
readArea();
}
function makeBounds() {
var minX = renderer.bounds.min.x;
var minZ = renderer.bounds.min.z;
var topY = renderer.bounds.size.y;
var unitX = renderer.bounds.size.x / 15;
var unitZ = renderer.bounds.size.z / 10;
var xCent = unitX / 2 + minX;
var zCent = unitZ / 2 + minZ;
var yCent = topY / 2;
var c = 1;
for (var i = 0; i < 150; i++) {
boundsArray.Push(Bounds(Vector3(xCent,yCent,zCent),Vector3(unitX,topY,unitZ)));
if (c == 15) {
xCent = unitX / 2 + minX;
zCent += unitZ;
c = 1;
} else {
xCent += unitX;
c++;
}
}
}
function makeTexts() {
for (var i = 0; i < 150; i++) {
textArray.Push("bounds" + i);
}
}
function readArea() {
for (i = 0; i < 150; i++) {
if (curBounds != i) {
if (boundsArray[i].Contains( eye.transform.position )) {
//t++;
//print(t);
curBounds = i;
print(textArray[i]);
break;
}
}
}
}
Here’s what I’m trying to do:
I have a large terrain which I want divided into regions that have individual phrases associated with them. The above script is attached to the terrain mesh. I used bounds stored in an array and test when the FPS controller (eye) enters a particular bound then print the phrase from the same index position in another array (which for now contains just dummy text).
What does work:
You’ll notice I have two lines commented out in the last function. If I uncomment them (and comment out the other print command) it appears that entering the bounds is detected because t increments and is printed faithfully whenever I move from region to region, and only when I move from region to region. It works even if I revisit bounds (see What doesn’t work, below). Also, with the code exactly as above, if I move around the terrain without visiting a bound a second time, then the associated phrases are printed correctly. I walked the perimeter of the terrain once and all was well.
What doesn’t work:
If I re-enter a bound, its associated phrase isn’t printed. If I move through a number of bounds, then revisit any of them, none of them trigger the print command.
Thanks for any help.