Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. // Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
  2. // Do test the code! You usually need to change a few small bits.
  3.  
  4. using UnityEngine;
  5. using System.Collections;
  6.  
  7. public class MYCLASSNAME : MonoBehaviour {
  8. ArrayList balls;
  9.  
  10. void  Start (){
  11.     balls = new ArrayList();
  12.    
  13.     // Instantiate ball for test-purpose
  14.     FIXME_VAR_TYPE newBall= Instantiate(ballPrefab,Vector3(10.0f,0.0f,10.0f),Quaternion.identity);
  15.     balls.Add(newBall.gameObject); // Notice .gameObject
  16. }
  17.  
  18. GameObject findClosestBall ( int ball_x ,   int ball_y  ){
  19.    
  20.     GameObject closestBall;
  21.     float shortestDistance = 9999;
  22.     Vector3 ballPos;
  23.     Vector3 coordinatePos;
  24.     Vector3 distanceVector;
  25.    
  26.     foreach(GameObject ball in balls){
  27.         ballPos = ball.transform.position;
  28.         coordinatePos = Vector3(ball_x, GameController.y_height, ball_y);
  29.         distanceVector = ballPos - coordinatePos;
  30.        
  31.         if (distanceVector.magnitude < shortestDistance){
  32.             closestBall = ball;
  33.             shortestDistance = distanceVector.magnitude;
  34.         }
  35.     }
  36.     return closestBall;
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement