Unity Enemy behaviour, unitybehaviour

Source: Internet
Author: User

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 ));
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.