Advertisement
alaslipknot

StandEnemy.cs

Aug 24th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class StandEnemy : MonoBehaviour
  5. {
  6.    
  7.    
  8.     private Camera          cam;                //game camera
  9.     private Transform       myTransform;            //transform cache  
  10.     public float            speed ;             //speed
  11.     public Vector3          targetPosition;         //target
  12.     private Vector3         tmpTarget;          //temporary target     
  13.     bool                hit;                //hit or not
  14.    
  15.     GameObject          ship;               //the ship object
  16.     ShipControl             shipControl;            //the ship script
  17.     GameObject          enemiesGenerator;       //the generator object 
  18.     Enemies             enemies;            //the generator script
  19.    
  20.     // Use this for initialization
  21.     void Start ()
  22.     {
  23.         //getting variables from the ship
  24.         ship = GameObject.Find ("Ship");
  25.         shipControl = ship.GetComponent<ShipControl> ();
  26.        
  27.         //getting variables from the enemies generator
  28.         enemiesGenerator = GameObject.Find ("Enemies Generator");
  29.         enemies = enemiesGenerator.GetComponent<Enemies> ();
  30.        
  31.         //setting values
  32.         myTransform = transform;
  33.         speed = 5;
  34.         targetPosition =
  35.         new Vector3 (shipControl.myTransform.position.x,shipControl.myTransform.position.y, 0);
  36.         hit = false;
  37.         if (cam == null) {
  38.             cam = Camera.main;
  39.        
  40.         }
  41.     }
  42.    
  43.    
  44.     /* TO-DO
  45.      * move to target (player)
  46.      * if hit another enemie
  47.      * > > move to random target
  48.      * if reach target
  49.      * > > go back to previous target
  50.      *
  51.      * */
  52.  
  53.    
  54.     // Update is called once per frame
  55.     void Update ()
  56.     {
  57.        
  58.         //target position has reached the random target (keep reading you'll get it )
  59.         if (targetPosition == tmpTarget)
  60.         {
  61.             hit = false;
  62.         }
  63.         //if there is a collision
  64.         if (hit)
  65.         {
  66.             //move the targetPosition to the random target (tmpTarget)
  67.             targetPosition = Vector3.MoveTowards (targetPosition, tmpTarget, speed * Time.deltaTime);
  68.         }
  69.             //if the targetPosition has reach the random target, then the player become the target
  70.         else  
  71.         {
  72.             targetPosition =Vector3.MoveTowards
  73.                 (targetPosition, shipControl.myTransform.position, speed * Time.deltaTime);
  74.         }
  75.         //move toward the target
  76.         myTransform.position = Vector3.MoveTowards
  77.                 (myTransform.position, targetPosition, speed * Time.deltaTime);
  78.         }
  79.  
  80.     void OnTriggerEnter (Collider collider)
  81.     {
  82.        
  83.         //if get shot
  84.         if (collider.gameObject.CompareTag ("Bullet"))
  85.         {
  86.             Destroy (gameObject);
  87.             print (gameObject.name);
  88.             enemies.standList.Remove (GameObject.Find (gameObject.name));
  89.         }
  90.        
  91.         // if collide with another enemy
  92.         if (collider.gameObject.CompareTag ("stand_enemy"))
  93.         {
  94.             hit = true;
  95.             tmpTarget = new Vector3 (Random.Range (-10, 10), Random.Range (-5, 5), 0);
  96.             Debug.Log (gameObject.name + ": " + targetPosition);
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement