Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class EagleMovement : MonoBehaviour
- {
- public Transform[] patrolPoints;
- public float speed;
- Transform currentPatrolPoint;
- int currentPatrolIndex;
- public Transform target1;
- public Transform target2;
- public float chaseRange;
- void Start()
- {
- currentPatrolIndex = 0;
- currentPatrolPoint = patrolPoints[currentPatrolIndex];
- GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionX;
- }
- void Update()
- {
- GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll;
- float distanceToTarget1 = Vector3.Distance(transform.position, target1.position);
- float distanceToTarget2 = Vector3.Distance(transform.position, target2.position);
- if (distanceToTarget1 < chaseRange)
- {
- GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.None;
- GetComponent<Rigidbody2D>().freezeRotation = false;
- Vector3 targetDir = target1.position - transform.position;
- float angle = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg - 90f;
- Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
- transform.rotation = Quaternion.RotateTowards(transform.rotation, q, 20);
- transform.Translate(Vector3.up * Time.deltaTime * speed);
- }
- if (distanceToTarget2 < chaseRange)
- {
- GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.None;
- GetComponent<Rigidbody2D>().freezeRotation = false;
- Vector3 targetDir = target2.position - transform.position;
- float angle = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg - 90f;
- Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
- transform.rotation = Quaternion.RotateTowards(transform.rotation, q, 20);
- transform.Translate(Vector3.up * Time.deltaTime * speed);
- }
- if (target1.position.x > transform.position.x && distanceToTarget1 < chaseRange)
- {
- transform.localScale = new Vector3(1, -1, 1);
- }
- else if (target1.position.x < transform.position.x && distanceToTarget1 < chaseRange)
- {
- transform.localScale = new Vector3(-1, -1, 1);
- }
- if (target2.position.x > transform.position.x && distanceToTarget2 < chaseRange)
- {
- transform.localScale = new Vector3(1, -1, 1);
- }
- else if (target2.position.x < transform.position.x && distanceToTarget2 < chaseRange)
- {
- transform.localScale = new Vector3(-1, -1, 1);
- }
- if (gameObject.transform.position.y == -50 || gameObject.transform.position.y < -50)
- {
- Destroy(gameObject);
- }
- if(distanceToTarget1 > chaseRange)
- {
- GetComponent<Rigidbody2D>().freezeRotation = true;
- }
- }
- void OnCollisionEnter2D(Collision2D collision)
- {
- if (collision.collider.tag == "Player")
- {
- StartCoroutine(Dying());
- if(collision.collider.name == "Player1")
- {
- StartCoroutine(Player1Hurt());
- }
- if(collision.collider.name == "Player2")
- {
- StartCoroutine(Player2Hurt());
- }
- }
- }
- IEnumerator Dying()
- {
- GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionX;
- GetComponent<BoxCollider2D>().enabled = false;
- GetComponent<Rigidbody2D>().AddForce(Vector2.right * 200);
- GetComponent<Rigidbody2D>().gravityScale = 10;
- yield return new WaitForSeconds(1);
- Destroy(gameObject);
- }
- IEnumerator Player1Hurt()
- {
- target1.GetComponent<Animator>().SetBool("IsHurt", true);
- yield return new WaitForSeconds(1/2);
- target1.GetComponent<Animator>().SetBool("IsHurt", false);
- }
- IEnumerator Player2Hurt()
- {
- target2.GetComponent<Animator>().SetBool("IsHurt", true);
- yield return new WaitForSeconds(1 / 2);
- target2.GetComponent<Animator>().SetBool("IsHurt", false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement