Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using DoodleStudio95;
- using UnityEngine;
- public class DungeonCat : MonoBehaviour {
- public float speed;
- private float timeBetweenAttacks;
- public float startTimeBetweenAttacks;
- public float stoppingDistance;
- public Transform player;
- public DoodleAnimationFile animationRetreating;
- public DoodleAnimationFile animationAttacking;
- DoodleAnimator animator;
- void Start()
- {
- animator = GetComponent<DoodleAnimator>();
- player = GameObject.FindGameObjectWithTag("Player").transform;
- timeBetweenAttacks = startTimeBetweenAttacks;
- }
- void Update()
- {
- float distToPlayer = Vector3.Distance(player.position, transform.position);
- Vector3 moveDirection = Vector3.zero;
- if (timeBetweenAttacks <= 0 && distToPlayer > 3 && Vector2.Distance(transform.position, player.position) > stoppingDistance)
- {
- transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
- animator.ChangeAnimation(animationAttacking);
- EnemyAttack();
- }
- else if (timeBetweenAttacks > 0)
- {
- transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
- EnemyRetreat();
- }
- else
- {
- timeBetweenAttacks -= Time.deltaTime;
- }
- float xDir = player.transform.position.x - transform.position.x;
- if (xDir < 0)
- {
- transform.localScale = new Vector2(2f, 2f);
- }
- if (xDir > 0)
- {
- transform.localScale = new Vector2(-2f, 2f);
- }
- }
- void EnemyRetreat()
- {
- speed = 2;
- transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
- animator.ChangeAnimation(animationRetreating);
- timeBetweenAttacks -= Time.deltaTime;
- }
- void EnemyAttack()
- {
- speed = 4;
- transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
- animator.ChangeAnimation(animationAttacking);
- Invoke("ResetTime", 4f);
- }
- private void ResetTime()
- {
- timeBetweenAttacks = startTimeBetweenAttacks;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement