Advertisement
Zuvi

SlimeControl.cs

May 26th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SlimeControll : MonoBehaviour
  6. {
  7.  
  8. public float speed;
  9. public float distance;
  10. public int life = 3;
  11. private bool movingRight = true;
  12. public Transform wallDetectionRight;
  13. public Transform wallDetectionLeft;
  14. RaycastHit2D wallInfo;
  15. [SerializeField] private LayerMask layerMask;
  16. private float defaultSpeed;
  17. bool ragemode = false;
  18. SpriteRenderer sprite;
  19. Color defaultColor;
  20. // Start is called before the first frame update
  21. void Start()
  22. {
  23. defaultSpeed = speed;
  24. life = 3;
  25. sprite = GetComponent<SpriteRenderer>();
  26. defaultColor = sprite.color;
  27. }
  28.  
  29. // Update is called once per frame
  30. void Update()
  31. {
  32. if (movingRight)
  33. {
  34. transform.Translate(Vector2.right * speed * Time.deltaTime);
  35. wallInfo = Physics2D.Raycast(wallDetectionRight.position, Vector2.right, distance, layerMask);
  36. }
  37. if (!movingRight)
  38. {
  39. transform.Translate(Vector2.left * speed * Time.deltaTime);
  40. wallInfo = Physics2D.Raycast(wallDetectionLeft.position, Vector2.left, distance, layerMask);
  41. }
  42.  
  43.  
  44. if (wallInfo.collider == true)
  45. {
  46. if (movingRight)
  47. {
  48. Flip();
  49. movingRight = false;
  50. }
  51. else
  52. {
  53. Flip();
  54. movingRight = true;
  55. }
  56. }
  57. }
  58. void OnTriggerEnter2D(Collider2D col)
  59. {
  60. if (col.CompareTag("bullet"))
  61. {
  62. life--;
  63. if (life <= 0)
  64. {
  65. Destroy(gameObject);
  66. } else if (ragemode == false)
  67. {
  68.  
  69. ragemode = true;
  70. speed = defaultSpeed + 4;
  71. GameSystem.Instance.PlaySound("slimeAttack", GameSystem.Instance.enemySounds);
  72. Invoke("RevertSpeed", 3);
  73. if (col.gameObject.transform.position.x > transform.position.x)
  74. {
  75. if (!movingRight)
  76. {
  77. movingRight = true;
  78. Flip();
  79. }
  80. }
  81. else if (col.gameObject.transform.position.x < transform.position.x)
  82. {
  83. if (movingRight)
  84. {
  85. movingRight = false;
  86. Flip();
  87. }
  88. }
  89.  
  90. }
  91. Blink();
  92. Destroy(col.gameObject);
  93. }
  94. }
  95. void RevertSpeed()
  96. {
  97. speed = defaultSpeed;
  98. ragemode = false;
  99. }
  100. void Flip()
  101. {
  102. Vector3 theScale = transform.localScale;
  103. theScale.x *= -1;
  104. transform.localScale = theScale;
  105. }
  106. void Blink()
  107. {
  108. sprite.material.color = Color.clear;
  109. Invoke("SetDefalutColor", 0.05f);
  110.  
  111. }
  112. void SetDefalutColor()
  113. {
  114. sprite.material.color = defaultColor;
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement