Hey guys, have a script here that substitutes my old one, because no one apparently knows what Arrays are here.
// the tag to search for (set this value in the inspector)
var searchTag = "Questions";
// the frequency with which to re-scane for new nearest target in seconds
// (set in inspector)
var scanFrequency = 0.1;
// the current target
private var target : Transform;
function Start() {
// set up repeating scan for new targets:
InvokeRepeating("ScanForTarget", 0, scanFrequency );
}
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 = GetNearestTaggedObject();
}
function GetNearestTaggedObject() : Transform {
// and finally the actual process for finding the nearest object:
var nearestDistanceSqr = Mathf.Infinity;
var taggedGameObjects = GameObject.FindGameObjectsWithTag(searchTag);
var nearestObj : Transform = null;
// loop through each tagged object, remembering nearest one found
for (var obj : GameObject in taggedGameObjects) {
var objectPos = obj.transform.position;
var distanceSqr = (objectPos - transform.position).sqrMagnitude;
if (distanceSqr < nearestDistanceSqr) {
nearestObj = obj.transform;
nearestDistanceSqr = distanceSqr;
}
}
return nearestObj;
}
function OnTriggerEnter(other : Collider){
if(other.tag == "QuestionCube"){
transform.LookAt(target);
}
}
Once again, no errors, no results.
Why isn’t this working?