private int STATE_IDLE=0;
private int STATE_HUNTING=1;
private int STATE_DEAD=2;
public int aiState;
public GameObject m_player;
private float waitTime;
private Vector3 idlePos;
// Use this for initialization
void Start ()
{
idlePos = this.GetComponent <Transform>().position;
aiState = STATE_IDLE;
}
public bool isEnemyIdle()
{
return (aiState == STATE_IDLE);
}
public void startHunting()
{
aiState = STATE_HUNTING;
}
// Update is called once per frame
void Update ()
{
if(aiState == STATE_HUNTING)
{
if(waitTime >= 0)
{
waitTime -= Time.deltaTime;
//Debug.Log ("time " + waitTime);
}
else
{
Vector3 playerPos = m_player.GetComponent<Transform>().position;
Vector3 pos = this.GetComponent<Transform>().position;
this.GetComponent<Transform>().position = Vector3.MoveTowards(pos, playerPos, 0.07f);
}
}
else if(aiState == STATE_DEAD)
{
if(waitTime >= 0)
{
waitTime -= Time.deltaTime;
}
else
{
reset ();
}
}
else
{
//Debug.Log ("Idle");
// DO NOTHING
}
}
private void reset()
{
aiState = STATE_IDLE;
this.GetComponent<Transform>().position = idlePos;
this.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
this.GetComponent<Transform>().rotation = Quaternion.Euler(new Vector3(0,0,0));
aiState = STATE_IDLE;
}
void OnTriggerEnter(Collider other)
{
if(aiState == STATE_DEAD )
{
return;
}
if(other.tag == "Player")
{
waitTime = 0.5f;
m_player.GetComponent<PlayerController>().playerHit(this.GetComponent<Transform>().position);
}
else if (other.tag == "bullet")
{
die (m_player.GetComponent<Transform>().position);
}
}
private void die(Vector3 playerPos)
{
waitTime = 2f;
aiState = STATE_DEAD;
Vector3 pos = this.GetComponent<Transform>().position;
this.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
this.GetComponent<Rigidbody>().AddForce(( pos - playerPos).normalized*600);
No comments:
Post a Comment