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 Enemy : MonoBehaviour
- {
- public GameObject turningPoint;
- public GameObject accidentTarget;
- public GameObject turningSpeedSetter;
- public float turningPointMoveSpeedUp;
- public float turningPointMoveSpeedDown;
- public float turningPointMoveSpeedRight;
- public float turningPointMoveSpeedLeft;
- public float moveSpeed = 10f;
- public float turningSpeed = 150f;
- public float rotatingSpeed = 150f;
- public float accidentSpeed;
- public bool turning = false;
- public bool driving = true;
- public bool moveTurningTargetUp = false;
- public bool moveTurningTargetDown = false;
- public bool turningLeft;
- public bool accident;
- public bool turningMode;
- public bool enemyAccident;
- public int number;
- Rigidbody2D rb;
- Animator animator;
- Vector3 currentRotation;
- Vector2 turningPointPos;
- Vector3 speedSetterDis;
- bool canSetTurningPoint = false;
- float disY;
- void Start()
- {
- animator = GetComponent<Animator>();
- rb = GetComponent<Rigidbody2D>();
- }
- void Update()
- {
- turningPointPos = turningPoint.transform.localPosition;
- turningPointPos.y = Mathf.Clamp(turningPointPos.y, -1.7f, 1.7f);
- turningPointPos.x = Mathf.Clamp(turningPointPos.x, -4.54f, 4.54f);
- turningPoint.transform.localPosition = turningPointPos;
- if (enemyAccident == true)
- {
- StartCoroutine(EnemyAccident());
- }
- Move();
- }
- private void Move()
- {
- if (accident == false)
- {
- if (driving == true)
- {
- transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
- }
- if (turning == true)
- {
- TurningTime();
- }
- if (turningMode == true)
- {
- if (turning == true)
- {
- driving = false;
- moveTurningTargetDown = false;
- moveTurningTargetUp = true;
- if (moveTurningTargetUp == true)
- {
- turningPoint.transform.Translate(Vector2.up * turningPointMoveSpeedUp * Time.deltaTime);
- turningPoint.transform.Translate(Vector2.right * turningPointMoveSpeedRight * Time.deltaTime);
- }
- if (turningLeft == true)
- {
- if (turningMode == true)
- {
- transform.Rotate(Vector3.forward, Time.deltaTime * rotatingSpeed);
- }
- }
- currentRotation = transform.localRotation.eulerAngles;
- currentRotation.z = Mathf.Clamp(currentRotation.z, 0, 200);
- transform.localRotation = Quaternion.Euler(currentRotation);
- }
- }
- if (turningMode == false)
- {
- if (turning == true)
- {
- moveTurningTargetDown = true;
- }
- }
- if (turning == true)
- {
- if (moveTurningTargetDown == true)
- {
- turningPoint.transform.Translate(Vector2.down * turningPointMoveSpeedDown * Time.deltaTime);
- turningPoint.transform.Translate(Vector2.left * turningPointMoveSpeedLeft * Time.deltaTime);
- }
- transform.RotateAround(turningPoint.transform.position, new Vector3(0, 0, 1), turningSpeed * Time.deltaTime);
- }
- SetTurningPoint();
- }
- }
- public void TurningTime()
- {
- StartCoroutine(Turning());
- }
- IEnumerator Turning()
- {
- disY = speedSetterDis.y;
- if (number == 0)
- {
- disY = disY * -1;
- print(disY);
- }
- if (disY <= 0.36f)
- {
- yield return new WaitForSeconds(0.01f);
- turningMode = true;
- yield return new WaitForSeconds(0.15f);
- turningMode = false;
- }
- if (disY <= 1.08f || disY <= 1.6f)
- {
- yield return new WaitForSeconds(0.2f);
- turningMode = true;
- yield return new WaitForSeconds(0.1f);
- turningMode = false;
- }
- }
- private void SetTurningPoint()
- {
- if (canSetTurningPoint == true)
- {
- if (number == 1)
- {
- turningPoint.transform.position = new Vector3(4.4f, 1.53f, 0);
- }
- else if (number == 0)
- {
- turningPoint.transform.position = new Vector3(-4.4f, -1.53f, 0);
- }
- }
- canSetTurningPoint = false;
- }
- IEnumerator EnemyAccident()
- {
- Accident();
- yield return new WaitForSeconds(0.2f);
- enemyAccident = false;
- }
- public void Accident()
- {
- accident = true;
- animator.SetBool("Accident", true);
- Vector2 target = accidentTarget.transform.position;
- transform.position = Vector2.MoveTowards(transform.position, target, accidentSpeed * Time.deltaTime);
- }
- private void OnTriggerEnter2D(Collider2D collision)
- {
- turningPointMoveSpeedUp = Random.Range(2, 3);
- turningPointMoveSpeedDown = Random.Range(4, 5);
- turningPointMoveSpeedRight = Random.Range(2, 3);
- turningPointMoveSpeedLeft = Random.Range(3, 5);
- if (collision.gameObject.tag == "TurningColliderON")
- {
- driving = false;
- turning = true;
- moveTurningTargetDown = true;
- turningLeft = true;
- transform.localRotation = Quaternion.Euler(0, 0, 0);
- }
- else if (collision.gameObject.tag == "TurningColliderOFF")
- {
- driving = true;
- turning = false;
- turningLeft = false;
- if (number == 0)
- {
- transform.localRotation = Quaternion.Euler(0, 0, 0);
- }
- else if (number == 1)
- {
- transform.localRotation = Quaternion.Euler(0, 0, 180);
- }
- }
- else if (collision.gameObject.tag == "RightTurn")
- {
- speedSetterDis = turningSpeedSetter.transform.position - transform.position;
- StartCoroutine(ChangeSpeedSetterPositionToUp());
- canSetTurningPoint = true;
- turningPoint.transform.localRotation = Quaternion.Euler(0, 0, 0);
- number = 1;
- }
- else if (collision.gameObject.tag == "LeftTurn")
- {
- speedSetterDis = turningSpeedSetter.transform.position - transform.position;
- StartCoroutine(ChangeSpeedSetterPositionToDown());
- canSetTurningPoint = true;
- turningPoint.transform.localRotation = Quaternion.Euler(0, 0, 180);
- number = 0;
- }
- else if (collision.gameObject.tag == "LoseCollider")
- {
- enemyAccident = true;
- }
- }
- IEnumerator ChangeSpeedSetterPositionToUp()
- {
- yield return new WaitForSeconds(2);
- turningSpeedSetter.transform.position = new Vector3(-2.5f, 2f, 0);
- }
- IEnumerator ChangeSpeedSetterPositionToDown()
- {
- yield return new WaitForSeconds(2);
- turningSpeedSetter.transform.position = new Vector3(2f, -2f, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement