Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PaddleScript : MonoBehaviour {
- string SearchTag = "Asteroids";
- float scanFrequency = 1.0f;
- Transform target;
- Quaternion rotation = Quaternion.identity;
- Vector3 radius = new Vector3(490,50,0);
- float currentRotation = 1;
- Vector3 centerPos = new Vector3(0,5,0);
- // Use this for initialization
- void Start () {
- InvokeRepeating ("ScanTarget", 0, scanFrequency);
- }
- // Update is called once per frame
- void LateUpdate () {
- currentRotation += 1 * Time.deltaTime * 10;
- // Get distance between two objects //
- if (target) {
- Vector3 distance = transform.InverseTransformPoint(target.position);
- if (distance.x < 0.0)
- {
- currentRotation += 1 * Time.deltaTime * 10;
- } else if (distance.x > 0.0)
- {
- currentRotation -= 1 * Time.deltaTime * 10;
- }
- }
- rotation.eulerAngles = new Vector3(0,currentRotation,0);
- transform.position = rotation * radius;
- transform.LookAt(centerPos);
- }
- void ScanTarget()
- {
- target = GetNearestTaggedObject();
- }
- Transform GetNearestTaggedObject()
- {
- float nearestDistanceSqr = Mathf.Infinity;
- GameObject[] objects = GameObject.FindGameObjectsWithTag("Asteroids");
- Transform nearestObject = null;
- foreach(GameObject obj in objects)
- {
- Vector3 objectPos = obj.transform.position;
- float distanceSqr = (objectPos - transform.position).sqrMagnitude;
- if (distanceSqr < nearestDistanceSqr)
- {
- nearestObject = obj.transform;
- nearestDistanceSqr = distanceSqr;
- }
- }
- return nearestObject;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement