• API
• FAQ
• Tools
• Archive
SHARE
TWEET

Untitled

a guest Feb 17th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. using System.Collections;
2. using System.Collections.Generic;
3. using UnityEngine;
4.
5. public class EagleMovement : MonoBehaviour
6. {
7.     public Transform[] patrolPoints;
8.     public float speed;
9.     Transform currentPatrolPoint;
10.     int currentPatrolIndex;
11.
12.     public Transform target1;
13.     public Transform target2;
14.     public float chaseRange;
15.
16.     void Start()
17.     {
18.         currentPatrolIndex = 0;
19.         currentPatrolPoint = patrolPoints[currentPatrolIndex];
20.         GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionX;
21.     }
22.
23.     void Update()
24.     {
25.         GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll;
26.         float distanceToTarget1 = Vector3.Distance(transform.position, target1.position);
27.         float distanceToTarget2 = Vector3.Distance(transform.position, target2.position);
28.         if (distanceToTarget1 < chaseRange)
29.         {
30.             GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.None;
31.             GetComponent<Rigidbody2D>().freezeRotation = false;
32.             Vector3 targetDir = target1.position - transform.position;
33.             float angle = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg - 90f;
34.             Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
35.             transform.rotation = Quaternion.RotateTowards(transform.rotation, q, 20);
36.             transform.Translate(Vector3.up * Time.deltaTime * speed);
37.         }
38.
39.         if (distanceToTarget2 < chaseRange)
40.         {
41.             GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.None;
42.             GetComponent<Rigidbody2D>().freezeRotation = false;
43.             Vector3 targetDir = target2.position - transform.position;
44.             float angle = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg - 90f;
45.             Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
46.             transform.rotation = Quaternion.RotateTowards(transform.rotation, q, 20);
47.             transform.Translate(Vector3.up * Time.deltaTime * speed);
48.         }
49.
50.         if (target1.position.x > transform.position.x && distanceToTarget1 < chaseRange)
51.         {
52.             transform.localScale = new Vector3(1, -1, 1);
53.         }
54.         else if (target1.position.x < transform.position.x && distanceToTarget1 < chaseRange)
55.         {
56.             transform.localScale = new Vector3(-1, -1, 1);
57.         }
58.
59.         if (target2.position.x > transform.position.x && distanceToTarget2 < chaseRange)
60.         {
61.             transform.localScale = new Vector3(1, -1, 1);
62.         }
63.         else if (target2.position.x < transform.position.x && distanceToTarget2 < chaseRange)
64.         {
65.             transform.localScale = new Vector3(-1, -1, 1);
66.         }
67.
68.         if (gameObject.transform.position.y == -50 || gameObject.transform.position.y < -50)
69.         {
70.             Destroy(gameObject);
71.         }
72.
73.         if(distanceToTarget1 > chaseRange)
74.         {
75.             GetComponent<Rigidbody2D>().freezeRotation = true;
76.         }
77.     }
78.
79.     void OnCollisionEnter2D(Collision2D collision)
80.     {
81.         if (collision.collider.tag == "Player")
82.         {
83.             StartCoroutine(Dying());
84.
85.             if(collision.collider.name == "Player1")
86.             {
87.                 StartCoroutine(Player1Hurt());
88.             }
89.
90.             if(collision.collider.name == "Player2")
91.             {
92.                 StartCoroutine(Player2Hurt());
93.             }
94.         }
95.     }
96.
97.     IEnumerator Dying()
98.     {
99.         GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionX;
100.         GetComponent<BoxCollider2D>().enabled = false;
102.         GetComponent<Rigidbody2D>().gravityScale = 10;
103.         yield return new WaitForSeconds(1);
104.         Destroy(gameObject);
105.     }
106.
107.     IEnumerator Player1Hurt()
108.     {
109.         target1.GetComponent<Animator>().SetBool("IsHurt", true);
110.         yield return new WaitForSeconds(1/2);
111.         target1.GetComponent<Animator>().SetBool("IsHurt", false);
112.     }
113.
114.     IEnumerator Player2Hurt()
115.     {
116.         target2.GetComponent<Animator>().SetBool("IsHurt", true);
117.         yield return new WaitForSeconds(1 / 2);
118.         target2.GetComponent<Animator>().SetBool("IsHurt", false);
119.     }
120. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top