Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class StandEnemy : MonoBehaviour
- {
- private Camera cam; //game camera
- private Transform myTransform; //transform cache
- public float speed ; //speed
- public Vector3 targetPosition; //target
- private Vector3 tmpTarget; //temporary target
- bool hit; //hit or not
- GameObject ship; //the ship object
- ShipControl shipControl; //the ship script
- GameObject enemiesGenerator; //the generator object
- Enemies enemies; //the generator script
- // Use this for initialization
- void Start ()
- {
- //getting variables from the ship
- ship = GameObject.Find ("Ship");
- shipControl = ship.GetComponent<ShipControl> ();
- //getting variables from the enemies generator
- enemiesGenerator = GameObject.Find ("Enemies Generator");
- enemies = enemiesGenerator.GetComponent<Enemies> ();
- //setting values
- myTransform = transform;
- speed = 5;
- targetPosition =
- new Vector3 (shipControl.myTransform.position.x,shipControl.myTransform.position.y, 0);
- hit = false;
- if (cam == null) {
- cam = Camera.main;
- }
- }
- /* TO-DO
- * move to target (player)
- * if hit another enemie
- * > > move to random target
- * if reach target
- * > > go back to previous target
- *
- * */
- // Update is called once per frame
- void Update ()
- {
- //target position has reached the random target (keep reading you'll get it )
- if (targetPosition == tmpTarget)
- {
- hit = false;
- }
- //if there is a collision
- if (hit)
- {
- //move the targetPosition to the random target (tmpTarget)
- targetPosition = Vector3.MoveTowards (targetPosition, tmpTarget, speed * Time.deltaTime);
- }
- //if the targetPosition has reach the random target, then the player become the target
- else
- {
- targetPosition =Vector3.MoveTowards
- (targetPosition, shipControl.myTransform.position, speed * Time.deltaTime);
- }
- //move toward the target
- myTransform.position = Vector3.MoveTowards
- (myTransform.position, targetPosition, speed * Time.deltaTime);
- }
- void OnTriggerEnter (Collider collider)
- {
- //if get shot
- if (collider.gameObject.CompareTag ("Bullet"))
- {
- Destroy (gameObject);
- print (gameObject.name);
- enemies.standList.Remove (GameObject.Find (gameObject.name));
- }
- // if collide with another enemy
- if (collider.gameObject.CompareTag ("stand_enemy"))
- {
- hit = true;
- tmpTarget = new Vector3 (Random.Range (-10, 10), Random.Range (-5, 5), 0);
- Debug.Log (gameObject.name + ": " + targetPosition);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement