Advertisement
Guest User

canonball

a guest
Feb 28th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. using System.Collections;
  4.  
  5.  
  6.  
  7. public class Projectile2 : MonoBehaviour
  8.  
  9. {
  10.  
  11.    
  12.  
  13.     public string type;
  14.  
  15.     public float speed;
  16.  
  17.     private GameObject checkNearestItem;
  18.  
  19.     private Enemy enemy;
  20.  
  21.  
  22.  
  23.     //Bezier Management
  24.  
  25.     private float startmeX = 0.0f;
  26.  
  27.     private float startmeX2 = 0.0f;
  28.  
  29.     private float startmeY = 0.0f;
  30.  
  31.     private float ControlPointX = 260.0f;
  32.  
  33.     private float ControlPointY = -10.0f;
  34.  
  35.     private float CurveX;
  36.  
  37.     private float CurveY;
  38.  
  39.     private float BezierTime = 0.0f;
  40.  
  41.  
  42.  
  43.  
  44.  
  45.    
  46.  
  47.  
  48.    
  49.  
  50.     // Use this for initialization
  51.  
  52.     void Start ()
  53.  
  54.     {
  55.  
  56.     startmeX = transform.position.x - 0.0f;
  57.  
  58.     startmeX2 = transform.position.x - 34.0f;
  59.  
  60.     startmeY = transform.position.y - 50.0f;
  61.  
  62.     }
  63.  
  64.    
  65.  
  66.    
  67.  
  68.  
  69.  
  70.    
  71.  
  72.     // Update is called once per frame
  73.  
  74.     void Update ()
  75.  
  76.     {
  77.  
  78.    
  79.         //Rotate the canonball projectile
  80.  
  81.         transform.Rotate (0,4,0);
  82.  
  83.        
  84.  
  85.            
  86.  
  87.        
  88.  
  89.        
  90.  
  91.        
  92.  
  93.        
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.         checkNearestItem = GameObject.Find(FindClosestEnemy().name);
  102.  
  103.        
  104.  
  105.        
  106.  
  107.        
  108.  
  109.         //Attack the nearest ENEMY 
  110.  
  111.         if (checkNearestItem != null)
  112.  
  113.         {
  114.  
  115.             BezierTime = BezierTime + Time.deltaTime;
  116.  
  117.  
  118.  
  119.             if (BezierTime >= 1)
  120.  
  121.             {
  122.  
  123.                 BezierTime = 0;
  124.  
  125.             Destroy(gameObject);
  126.  
  127.             }
  128.  
  129.            
  130.  
  131.             CurveX = (((1-BezierTime)*(1-BezierTime)) * startmeX) + (2 * BezierTime * (1 - BezierTime) * (((checkNearestItem.transform.position.x - 0.0f) - startmeX) / 2.0f + startmeX)) + ((BezierTime * BezierTime) * (checkNearestItem.transform.position.x - 0.0f));
  132.  
  133.                 CurveY = (((1-BezierTime)*(1-BezierTime)) * startmeY) + (2 * BezierTime * (1 - BezierTime) * ControlPointY) + ((BezierTime * BezierTime) * checkNearestItem.transform.position.y);
  134.  
  135.                 transform.position = new Vector3(CurveX, CurveY, 1);
  136.  
  137.         }
  138.  
  139.                
  140.  
  141.                
  142.  
  143.  
  144.  
  145.  
  146.  
  147.     //End of Update()  
  148.  
  149.     }
  150.  
  151.    
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.    
  160.  
  161.         GameObject FindClosestEnemy () {
  162.  
  163.             GameObject[] gos;
  164.  
  165.             gos = GameObject.FindGameObjectsWithTag("Croc");
  166.  
  167.             GameObject closest;
  168.  
  169.             float distance = Mathf.Infinity;
  170.  
  171.             Vector3 position = transform.position;
  172.  
  173.             foreach (GameObject go in gos) {
  174.  
  175.            
  176.             //List only enemies whose Y position is equal to 90.
  177.  
  178.             if (go.transform.position.y == 90)
  179.  
  180.             {
  181.  
  182.                     Vector3 diff = go.transform.position - position;
  183.  
  184.                     float curDistance = diff.sqrMagnitude;
  185.  
  186.                     if (curDistance < distance)
  187.                     {
  188.  
  189.                             closest = go;
  190.  
  191.                             distance = curDistance;
  192.  
  193.                     }
  194.  
  195.             }
  196.  
  197.            
  198.         }
  199.  
  200.         return closest;
  201.  
  202.     }
  203.  
  204.    
  205.  
  206.    
  207.  
  208.    
  209.  
  210.     public Vector3 GetQuadraticCoordinates(float t, Vector3 p0, Vector3 c0, Vector3 p1)
  211.  
  212.     {
  213.  
  214.         return Mathf.Pow(1-t,2)*p0 + 2*t*(1-t)*c0 + Mathf.Pow(t,2)*p1;
  215.  
  216.     }
  217.  
  218.    
  219.  
  220.    
  221.  
  222.    
  223.  
  224.  
  225.  
  226.    
  227.  
  228.    
  229.  
  230.    
  231.  
  232.    
  233.  
  234.    
  235.  
  236.    
  237.  
  238.    
  239.  
  240.    
  241.  
  242.    
  243.  
  244.    
  245.  
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement