Advertisement
Guest User

Untitled

a guest
May 28th, 2012
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PaddleScript : MonoBehaviour {
  5.    
  6.     string SearchTag = "Asteroids";
  7.    
  8.     float scanFrequency = 1.0f;
  9.    
  10.     Transform target;
  11.    
  12.     Quaternion rotation = Quaternion.identity;
  13.    
  14.     Vector3 radius = new Vector3(490,50,0);
  15.    
  16.     float currentRotation = 1;
  17.    
  18.     Vector3 centerPos = new Vector3(0,5,0);
  19.    
  20.     // Use this for initialization
  21.     void Start () {
  22.         InvokeRepeating ("ScanTarget", 0, scanFrequency);
  23.     }
  24.    
  25.     // Update is called once per frame
  26.     void LateUpdate () {
  27.         currentRotation += 1 * Time.deltaTime * 10;
  28.        
  29.        
  30.         // Get distance between two objects //
  31.        
  32.         if (target) {
  33.             Vector3 distance = transform.InverseTransformPoint(target.position);
  34.            
  35.             if (distance.x < 0.0)
  36.             {
  37.                 currentRotation += 1 * Time.deltaTime * 10;
  38.             } else if (distance.x > 0.0)
  39.             {
  40.                 currentRotation -= 1 * Time.deltaTime * 10;
  41.             }
  42.         }
  43.        
  44.         rotation.eulerAngles = new Vector3(0,currentRotation,0);
  45.        
  46.         transform.position = rotation * radius;
  47.        
  48.         transform.LookAt(centerPos);
  49.     }
  50.    
  51.     void ScanTarget()
  52.     {
  53.         target = GetNearestTaggedObject();
  54.     }
  55.    
  56.     Transform GetNearestTaggedObject()
  57.     {
  58.         float nearestDistanceSqr = Mathf.Infinity;
  59.        
  60.         GameObject[] objects = GameObject.FindGameObjectsWithTag("Asteroids");
  61.        
  62.         Transform nearestObject = null;
  63.        
  64.         foreach(GameObject obj in objects)
  65.         {
  66.             Vector3 objectPos = obj.transform.position;
  67.             float distanceSqr = (objectPos - transform.position).sqrMagnitude;
  68.            
  69.             if (distanceSqr < nearestDistanceSqr)
  70.             {
  71.                 nearestObject = obj.transform;
  72.                 nearestDistanceSqr = distanceSqr;
  73.                
  74.             }
  75.         }
  76.        
  77.         return nearestObject;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement