- Home /
Accessing boolean properties from array of transforms
I am trying to make a script in which the characters( goats ) jump from spot to spot. I have an array of transforms which are jumpspots. The thing is that I only want them to jump to spots that are unoccupied so I am wondering how to do that. I have tried making another script for each transform with a boolean that indicates its state(full/empty). I want to make ScanForTarget function to only look for targets that are empty. I imagine that I need to make a new array ( one that has only empty spots )from the possibleTargets array but am not sure how to proceed.I could not work out how to check the possibleTargets boolean states. Also , how can I make to the (transform.LookAt(target);) move through a smooth rotation?it is instantaneous at present. Thanks.
var front_switch : boolean;
var customButton : GUIStyle;
var possibleTargets : Transform[];
var audioGoatObject : GameObject;
var other : Rigidbody;
var searchTag = "Respawn";
var smoothTime = 0.3;
private var velocity = Vector3.zero;
// the frequency with which to re-scane for new nearest target in seconds
// (set in inspector)
var scanFrequency = 1.0;
// the current target
var target : Transform;
function Update() {
// we rotate to look at the target every frame (if there is one)
if (front_switch==false){
transform.LookAt(target);
var targetPosition : Vector3 = target.TransformPoint(Vector3(0, 0,0));
other.useGravity = false;
// Smoothly move the camera towards that target position
transform.position = Vector3.SmoothDamp(transform.position, targetPosition,velocity, smoothTime);
}
}
function ScanForTarget() {
// this should be called less often, because it could be an expensive
// process if there are lots of objects to check against
target = possibleTargets[Random.Range(0, possibleTargets.length)].transform;
}
function OnGUI(){
if(GUI.Button(Rect(50,Screen.height -150,Screen.width/10,Screen.width/10),"",customButton))
{
ScanForTarget();
jump();
audiojump();
}
}
function jump(){
animation.Play("jump");
yield WaitForSeconds(1.5);
animation.CrossFadeQueued("loop_goat");
}
function audiojump(){
audioGoatObject.audio.enabled=true;
yield WaitForSeconds(1);
audioGoatObject.audio.enabled=false;
}
Answer by chainedlupine · May 02, 2012 at 03:54 AM
There are many ways to skin this goat. (Ha! See what I did there?)
Is there a particular reason you are using an array of Transforms? I presume because these are actually GameObjects that exist in your Unity scene so they can be easily manipulated. If so, you can attach a component that contains the necessary bookkeeping information. So, create a script called JumpSpot and then assign it to the jump spot GameObjects. Then in your script, access it such as:
target.GetComponent(JumpSpot).isAvailable = false
Or you could track the states in an array, as you mentioned.
Or you could, instead of storing GameObjects, store some specialized data structure -- a class that contained a Vector3 point plus a reference to whether the spot was open, or black or green or upside down or whatever else state information you wanted to store.