Advertisement
Guest User

Untitled

a guest
Aug 5th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. using System.Collections; //Unity
  2. using System.Collections.Generic; //Unity
  3. using UnityEngine; //UnityEngine namespace
  4.  
  5. public class Enemy : MonoBehaviour
  6. {
  7. [SerializeField] Sprite[] SpriteType; //Create array field called "enemyType" to assign the enemy sprite
  8. [SerializeField] Vector3 destination = new Vector3(5.0f, 5.0f, 0f); //Create a point that the enemy will move to
  9. [SerializeField] float enemySpeed = 1f; //Set the enemy movement speed
  10. [SerializeField] AudioClip VictorySound;
  11. int EnemyType;
  12. Player Player;
  13.  
  14.  
  15. private void Start()
  16. {
  17. int EnemyType = Random.Range(0, 3); //Choose random integer in range to assign as enemy type between 0 and 2
  18. GetComponent<SpriteRenderer>().sprite = SpriteType[EnemyType]; //Set the sprite as the integer value of EnemyType
  19. }
  20.  
  21.  
  22.  
  23. void Update() //Run per frame
  24. {
  25.  
  26. var speedPerFrame = enemySpeed * Time.deltaTime; //Set the speed per frame by multiplying the enemy speed by delta time
  27. transform.position = Vector2.MoveTowards(transform.position, destination, speedPerFrame); //Make the enemy move towards something
  28. }
  29.  
  30. private void OnTriggerEnter2D(Collider2D Other) //When the enemy collides with the player
  31. {
  32. if (Other.gameObject.name == "Player") //If the other game object is the player
  33. {
  34. int playerValue = Other.GetComponent<Player>().ReadPlayerValue(); //Get player value from Player method "ReadPlayerValue"
  35. if ((EnemyType == 0) && ((playerValue == 0) || (playerValue == 1))) //If Enemy is paper, and player is paper or rock
  36. {
  37. FindObjectOfType<GameSystem>().endGame(); //End game
  38. }
  39. if ((EnemyType == 1) && ((playerValue == 1) || (playerValue == 2))) //If enemy is rock, and player is rock or scissors
  40. {
  41. FindObjectOfType<GameSystem>().endGame(); //End game
  42. }
  43. if ((EnemyType == 2) && ((playerValue == 2) || (playerValue == 0))) //If enemy is scissor, and player is scissor or paper
  44. {
  45. FindObjectOfType<GameSystem>().endGame(); //End game
  46. }
  47. else
  48. {
  49. AudioSource.PlayClipAtPoint(VictorySound, Camera.main.transform.position); //Play sound effect at location
  50. Destroy(gameObject); //Destroy enemy
  51. }
  52. }
  53. }
  54.  
  55.  
  56.  
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement