Skip to content

Instantly share code, notes, and snippets.

@yjsoon
Created June 4, 2015 02:36
Show Gist options
  • Save yjsoon/1773ef7a26791de8dce0 to your computer and use it in GitHub Desktop.
Save yjsoon/1773ef7a26791de8dce0 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterScript))]
public class EnemyAI : MonoBehaviour {
public enum EnemyState
{
searching,
following
}
//time in seconds till the character determines a new heading.
public float timeToChangeDirection=5.0f;
//amount of damage to do to the player
public int damage=5;
//how fast this can deal damage
public float damageFrequency=1.0f;
//internal timer to track last time dealt damage.
private float damageTimer;
//reference to the character script.
private CharacterScript character;
//internal timer to keep track of elapsed time.
private float internalTimer=0.0f;
//direction relative to the character to head.
private Vector2 heading;
private EnemyState currentState;
private GameObject target;
/// <summary>
/// Used for initializing your script component
/// </summary>
private void Awake () {
//cache the character script
this.character=this.GetComponent<CharacterScript>();
//set an initial heading.
this.createNewHeading();
//set internal timer for damagefrequency
this.damageTimer=this.damageFrequency;
this.currentState=EnemyState.searching;
}
/// <summary>
/// called after all awakes are called.
/// </summary>
private void Start()
{
/*
this.character.SetDeathFunction( () => {
//destroy the game object.
Destroy(this.gameObject);
});
*/
}
/// <summary>
/// Update that is called once per frame.
/// </summary>
private void Update () {
//increment our damage timer by the time it took to complete the last frame.
this.damageTimer += Time.deltaTime;
}
/// <summary>
/// Update that is kept in sync with physics.
/// </summary>
private void FixedUpdate()
{
//if we are searching, call the search function
if(this.currentState == EnemyState.searching)
{
this.Search();
} else if(this.currentState==EnemyState.following)
{
//if we are following, call the follow function
this.Follow();
}
}
/// <summary>
/// The enemy follows the player.
/// </summary>
private void Follow()
{
//vector math says position to go minus current position = vector we should head in.
Vector3 nTransform = this.target.transform.position - this.transform.position;
//make sure its of magnitude 1.
nTransform.Normalize();
//move that direction
character.Move(nTransform);
}
/// <summary>
/// The enemy searches for the player.
/// </summary>
private void Search()
{
//increase the internal timer by
//the amount of time that has passed.
this.internalTimer+=Time.deltaTime;
//determine if we need to create a new heading.
if (this.internalTimer > this.timeToChangeDirection)
{
//create a new heading
this.createNewHeading();
//reset the timer.
this.internalTimer=0.0f;
}
//move the character in the direction of its heading
character.Move(heading);
}
/// <summary>
/// Generates a new random heading for the character
/// and applies that heading immediately.
/// </summary>
private void createNewHeading()
{
//I used -100 to 100 to give an equal chance for any direction
float xCoord=Random.Range(-100.0f, 100.0f);
float yCoord=Random.Range(-100.0f, 100.0f);
//set our heading to the random headings.
heading = new Vector2(xCoord, yCoord);
//make sure the magnitude of our heading is 1
heading.Normalize();
}
/// <summary>
/// Called when a collision happens between the character
/// and something else.
/// </summary>
/// <param name=“other”>What you collided with.</param>
private void OnCollisionEnter2D(Collision2D other)
{
//make sure we don’t do too much damage to the player
if (other.gameObject.tag=="Player" && this.damageTimer > this.damageFrequency)
{
//deal some damage!
// other.gameObject.GetComponent<CharacterScript>().AdjustHealth(-1*this.damage);
//reset the timer so we don’t damage too much again.
this.damageTimer=0.0f;
}
//create a new heading, you ran into something.
this.createNewHeading();
}
public void SetSpawnedBy(GameObject o)
{
// this.spawnedBy=o;
}
public GameObject GetSpawnedBy()
{
// return this.spawnedBy;
return null;
}
/// <summary>
/// Occurs when something enters its trigger zone.
/// </summary>
/// <param name=“other”></param>
private void OnTriggerEnter2D(Collider2D other)
{
//check to see what this thing is
if (other.gameObject.tag=="Player")
{
//Its the player! FOLLOW IT!
this.currentState=EnemyState.following;
//set the target.
this.target=other.gameObject;
}
}
/// <summary>
/// Occurs when something exits its trigger zone.
/// </summary>
/// <param name=“other”></param>
private void OnTriggerExit2D(Collider2D other)
{
//Check to see what it is
if (other.gameObject.tag=="Player")
{
//Its the player, I can’t see it anymore, stop following.
this.currentState=EnemyState.searching;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment