Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections; //Unity
- using System.Collections.Generic; //Unity
- using UnityEngine; //UnityEngine namespace
- public class Enemy : MonoBehaviour
- {
- [SerializeField] Sprite[] SpriteType; //Create array field called "enemyType" to assign the enemy sprite
- [SerializeField] Vector3 destination = new Vector3(5.0f, 5.0f, 0f); //Create a point that the enemy will move to
- [SerializeField] float enemySpeed = 1f; //Set the enemy movement speed
- [SerializeField] AudioClip VictorySound;
- int EnemyType;
- Player Player;
- private void Start()
- {
- int EnemyType = Random.Range(0, 3); //Choose random integer in range to assign as enemy type between 0 and 2
- GetComponent<SpriteRenderer>().sprite = SpriteType[EnemyType]; //Set the sprite as the integer value of EnemyType
- }
- void Update() //Run per frame
- {
- var speedPerFrame = enemySpeed * Time.deltaTime; //Set the speed per frame by multiplying the enemy speed by delta time
- transform.position = Vector2.MoveTowards(transform.position, destination, speedPerFrame); //Make the enemy move towards something
- }
- private void OnTriggerEnter2D(Collider2D Other) //When the enemy collides with the player
- {
- if (Other.gameObject.name == "Player") //If the other game object is the player
- {
- int playerValue = Other.GetComponent<Player>().ReadPlayerValue(); //Get player value from Player method "ReadPlayerValue"
- if ((EnemyType == 0) && ((playerValue == 0) || (playerValue == 1))) //If Enemy is paper, and player is paper or rock
- {
- FindObjectOfType<GameSystem>().endGame(); //End game
- }
- if ((EnemyType == 1) && ((playerValue == 1) || (playerValue == 2))) //If enemy is rock, and player is rock or scissors
- {
- FindObjectOfType<GameSystem>().endGame(); //End game
- }
- if ((EnemyType == 2) && ((playerValue == 2) || (playerValue == 0))) //If enemy is scissor, and player is scissor or paper
- {
- FindObjectOfType<GameSystem>().endGame(); //End game
- }
- else
- {
- AudioSource.PlayClipAtPoint(VictorySound, Camera.main.transform.position); //Play sound effect at location
- Destroy(gameObject); //Destroy enemy
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement