Checking for closest Vector3 to player out of an array of them

I’m having some trouble. I’m not too experienced with Arrays and I’m trying to do some particular stuff.

The ultimate goal of my script is that I will be gravitating a player-controlled sphere towards certain marked objects, allowing it to walk across its surface such as obstacles seen in Super Mario Galaxy.

http://forums.epicgames.com/threads/945883-Video-Mario-Galaxy-Gravity-amp-Relative-Point-Gravity-Cling-to-Shapes-With-No-Center

The above link is the inspiration for this script, though its written for Unreal and doesn’t translate well to Unity Javascript.

The current problem I’m having comes with trying to find the nearest surface from a series of raycast hits, which I have arranged into arrays.

var Magnetize : boolean = false;
var gravity : int = 9.81;
var Speed : float =3;
var maxspeed: float =6;
var TurnSpeed: float = 180;
var nearestHit: RaycastHit;
var CastDirections : int = 16;
var DistanceOfCast : int = 10;
var RayGuns: Transform[];
var MagnetRange : float = 1;
private var MagnetPoints = new Array();
private var MagnetPointNormals = newArray();

function Start(){
 // Add a entry, it will resize it to one and assign it.
}


function FixedUpdate(){
	if (!Magnetize){
   		 // apply constant weight force according to character normal:
    		rigidbody.AddForce(-gravity*rigidbody.mass*Vector3.up);
   		 }
   		 
   	//if(Magnetize){
   		//rigidbody.AddForce(-gravity*rigidbody.mass*Vector3.up);

		
	rigidbody.AddForce(0, 0, Input.GetAxis("Vertical")*Speed*Time.deltaTime);
	rigidbody.AddForce(Input.GetAxis("Horizontal")*TurnSpeed*Time.deltaTime, 0, 0);
	if(Input.GetKey("z")){
		//stuff
		}
	}

function Update(){
	AcquireClosestSurface();
	//GravitateToClosestSurface(); /*This will pull the player towards the object normal.*/
	MagnetPoints.Clear();
}


function AcquireClosestSurface(){
	var currentclosestray : Vector3;
	for (var RayGun in RayGuns){
		Debug.DrawRay(RayGun.transform.position, RayGun.transform.up*MagnetRange,Color.green);
		var ray : Ray;
		var hit: RaycastHit;
    	ray = Ray(RayGun.transform.position, RayGun.transform.up);
    	if (Physics.Raycast(ray, hit, MagnetRange)){ // wall ahead?
	    	MagnetPoints.Push(hit.point);
	    	//MagnetPointNormals.Push(hit.normal); /* I can re-cast from the player to the hit.point to determine the normal later*/
	        print("Point Added");
	    }
	}
	Debug.Log(MagnetPoints);
	/*this is where you will determine the shortest distance*/
	currentclosestray = MagnetPoints[1];
	}
}

So my problem is actually two-fold. I’m not sure exactly how to calculate the distance from the player to the hit.point (I’m assuming some kind of Vector3.distance method), and once I’m able to get that, how do I sort through the array, pick out the closest hit.point, and then use it for stuff?

Written with no accuracy guarantee:

var closestIndex : int =0;
var closestDistance : float = 100000 //something big
for (var i=0; i<MagnetPoints.Length; i++){
  var distance : float = (transform.position - MagnetPoints[i]).magnitude;
  //You can use sqrMagnitude for a slightly faster calculation, if you do not actually need to know the distance, only which one is smallest

  if(distance < closestDistance){
    closestDistance = distance;
    closestIndex = i;
  }
}

//After this, MagnetPoints[closestIndest] is the nearest point

EDIT: Got it to work! referring to the index number was unnecessary when I can just use the same thing to return the vector3 point itself. Here’s what I used instead:

	var closestPointHit : Vector3;
	var closestDistance : float = 100000; //something big
	for (var indexPoint in MagnetPoints){
  		var distance : float = (transform.position - indexPoint).magnitude;
 		//You can use sqrMagnitude for a slightly faster calculation, if you do not actually need to know the distance, only which one is smallest
	 	if(distance < closestDistance){
	    	closestDistance = distance;
	    	closestPointHit = indexPoint;
 		 }
	}
	//After this, MagnetPoints[closestIndex] is the nearest point hit
	print (closestPointHit);
	return closestPointHit;
}

Thanks for your help! I’m really rusty on arrays and this saved me a lot of time and heartache.