Unity Enemy behaviour, unitybehaviour
Using UnityEngine;
Using System. Collections;
Public class enemyBehaviour: MonoBehaviour
{
// Enemy that moves to player when close enough
Public float speed = 6f;
Public Transform player;
Public float enemySight = 1000f;
Public AudioClip [] clips;
Public AudioSource source;
Int rand;
Static private int hohoindex = 8;
Static private int deathindex = 11;
// Use this for initialization
Void Start ()
{
Rand = Random. Range (0, hohoindex );
Source. clip = clips [rand];
}
// Update is called once per frame
Void FixedUpdate ()
{
If (! Puller_control.isDead ){
Player = GameObject. Find ("Player"). transform;
// Move and face towards player
Var heading = player. position-transform. position;
Var distance = heading. magnstance;
Var direction = heading/distance;
If (heading. sqrMagnitude <enemySight * enemySight ){
Float angle = Mathf. Atan2 (heading. y, heading. x) * Mathf. Rad2Deg;
Quaternion q = Quaternion. AngleAxis (angle, Vector3.forward );
// Transform. rotation = Quaternion. Slerp (transform. rotation, q, Time. deltaTime * speed );
Transform. position = Vector2.MoveTowards (rigidbody2D. position, new Vector2 (player. transform. position. x, player. transform. position. y), Time. deltaTime * speed );
}
}
StartCoroutine ("HoHo ");
}
// Die on collision with sled
Void OnCollisionEnter2D (Collision2D collision ){
If (collision. gameObject. name = "Sled "){
If (collision. relativeVelocity. magnity> 3)
{
Source. Stop ();
Source. clip = clips [Random. Range (hohoindex, deathindex)];
AudioSource. PlayClipAtPoint (source. clip, this. transform. position );
Destroy (this. gameObject );
}
}
If (collision. gameObject. name = "Player "){
Puller_control.isDead = true;
Destroy (player. gameObject );
Application. LoadLevel ("GameOver ");
}
}
// Coroutine to call the enemy's hohos.
IEnumerator HoHo (){
If (! Source. isPlaying ){
Rand = Random. Range (0, hohoindex );
Source. clip = clips [rand];
Source. Play ();
Yield return new WaitForSeconds (Random. Range (5, 10 ));
}
}
}