Skip to content

Instantly share code, notes, and snippets.

@jquave
Created August 7, 2012 19:24
Show Gist options
  • Save jquave/3288600 to your computer and use it in GitHub Desktop.
Save jquave/3288600 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class ReaperAI : MonoBehaviour {
int S_IDLE = 0;
int S_ROAM = 1;
int S_CHASE = 2;
int S_GIVEUP = 3;
public int currentSchedule = 0;
//double lastStartedChase = 0;
double lastScheduleSwitch;
double nextScheduleSwitch;
bool walkingRight = true;
Vector3 lastDistToMack;
BoxCollider visionCollider;
CharacterController character;
GameObject currentTarget;
// Use this for initialization
void Start () {
lastScheduleSwitch = Time.time;
nextScheduleSwitch = Time.time+2;
visionCollider = GetComponent<BoxCollider>();
character = GetComponent<CharacterController>();
}
void setAnimations() {
switch(currentSchedule) {
case 0: // S_IDLE
animation.CrossFade("idle",0.1f);
transform.LookAt(transform.position+new Vector3(0,0,-1));
break;
case 1: // S_ROAM
animation.CrossFade("run",0.1f);
if(character.velocity.x>0 && character.velocity.x!=0) transform.LookAt(transform.position+new Vector3(1,0,0));
else if(character.velocity.x!=0) transform.LookAt(transform.position+new Vector3(-1,0,0));
break;
case 2: // S_CHASE
if(lastDistToMack.magnitude>2)
animation.CrossFade("sprint",0.1f);
else animation.CrossFade("attack",0.1f);
if(character.velocity.x>0 && character.velocity.x!=0) transform.LookAt(transform.position+new Vector3(1,0,0));
else if(character.velocity.x!=0) transform.LookAt(transform.position+new Vector3(-1,0,0));
break;
case 3: // S_GIVEUP
animation.CrossFade("idle",0.1f);
transform.LookAt(transform.position+new Vector3(0,0,-1));
break;
default:
break;
}
}
// Update is called once per frame
void Update () {
if(Time.time>nextScheduleSwitch) {
nextScheduleSwitch = Time.time+2;
switch(currentSchedule) {
case 0: // S_IDLE
// Switch to roaming around now
currentSchedule = S_ROAM;
walkingRight = !walkingRight;
break;
case 1: // S_ROAM
currentSchedule = S_IDLE;
break;
case 2: // S_CHASE
currentSchedule = S_GIVEUP;
break;
case 3: // S_GIVEUP
// Switch to roaming around now
currentSchedule = S_ROAM;
walkingRight = !walkingRight;
break;
default:
break;
}
}
lastDistToMack = new Vector3(1,1,1);
if(currentTarget) {
lastDistToMack = currentTarget.transform.position - transform.position;
}
//float distToMack = 5;
Vector3 movement = new Vector3(0,0,0);
if(currentSchedule == S_ROAM) {
if(walkingRight) movement.x = 5;
else movement.x = -5;
}
if(currentSchedule == S_CHASE) {
if(lastDistToMack.magnitude>2) {
if(!walkingRight) movement.x = 17*lastDistToMack.normalized.x;
else movement.x = -17*lastDistToMack.normalized.x;
}
else {
//Mack should die now
//PlayerController iMack = currentTarget.GetComponent<PlayerController>();
//currentTarget.velocity = new Vector3(0,100,0);
//character = GetComponent<CharacterController>();
}
}
if(!character.isGrounded) {
// Not touching ground
movement.y = Physics.gravity.y*0.5f;
}
//if(currentSchedule!=S_CHASE) {
// Not chasing anyone at the moment, check around for candidates
//}
character.Move(movement*Time.deltaTime);
setAnimations();
}
void OnTriggerEnter (Collider other) {
// Something touched me, WHO IS TOUCHING ME?!?!
if(other.gameObject.tag=="Player") {
// IT'S A FUCKING PLAYER, STAB TIME!
currentTarget = other.gameObject;
currentSchedule = S_CHASE;
lastScheduleSwitch = Time.time;
nextScheduleSwitch = Time.time+15;
}
}
void OnTriggerExit (Collider other) {
nextScheduleSwitch = Time.time+5;
}
void OnCollisionEnter(Collision collision) {
// Debug-draw all contact points and normals
//for (var contact : ContactPoint in collision.contacts) {
foreach( ContactPoint contact in collision.contacts) {
Debug.DrawRay(contact.point, contact.normal, Color.white);
Debug.Log("Done collided!");
}
// Play a sound if the coliding objects had a big impact.
//if (collision.relativeVelocity.magnitude > 2)
//audio.Play();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment