Advertisement
Guest User

Untitled

a guest
May 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using DoodleStudio95;
  4. using UnityEngine;
  5.  
  6. public class DungeonCat : MonoBehaviour {
  7.  
  8. public float speed;
  9.  
  10. private float timeBetweenAttacks;
  11. public float startTimeBetweenAttacks;
  12.  
  13. public float stoppingDistance;
  14.  
  15. public Transform player;
  16.  
  17. public DoodleAnimationFile animationRetreating;
  18. public DoodleAnimationFile animationAttacking;
  19. DoodleAnimator animator;
  20.  
  21. void Start()
  22. {
  23. animator = GetComponent<DoodleAnimator>();
  24. player = GameObject.FindGameObjectWithTag("Player").transform;
  25. timeBetweenAttacks = startTimeBetweenAttacks;
  26. }
  27.  
  28. void Update()
  29. {
  30. float distToPlayer = Vector3.Distance(player.position, transform.position);
  31.  
  32. Vector3 moveDirection = Vector3.zero;
  33.  
  34. if (timeBetweenAttacks <= 0 && distToPlayer > 3 && Vector2.Distance(transform.position, player.position) > stoppingDistance)
  35. {
  36. transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
  37. animator.ChangeAnimation(animationAttacking);
  38. EnemyAttack();
  39. }
  40. else if (timeBetweenAttacks > 0)
  41. {
  42. transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
  43. EnemyRetreat();
  44. }
  45. else
  46. {
  47. timeBetweenAttacks -= Time.deltaTime;
  48. }
  49.  
  50. float xDir = player.transform.position.x - transform.position.x;
  51.  
  52. if (xDir < 0)
  53. {
  54. transform.localScale = new Vector2(2f, 2f);
  55. }
  56.  
  57. if (xDir > 0)
  58. {
  59. transform.localScale = new Vector2(-2f, 2f);
  60. }
  61. }
  62.  
  63. void EnemyRetreat()
  64. {
  65. speed = 2;
  66. transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
  67. animator.ChangeAnimation(animationRetreating);
  68. timeBetweenAttacks -= Time.deltaTime;
  69. }
  70.  
  71. void EnemyAttack()
  72. {
  73. speed = 4;
  74. transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
  75. animator.ChangeAnimation(animationAttacking);
  76. Invoke("ResetTime", 4f);
  77. }
  78.  
  79. private void ResetTime()
  80. {
  81. timeBetweenAttacks = startTimeBetweenAttacks;
  82. }
  83.  
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement