Advertisement
lemansky

Untitled

Apr 11th, 2021
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Enemy : MonoBehaviour
  6. {
  7.     public Transform pointA = null, pointB = null;
  8.     public float speed = 3.0f;
  9.     bool _switch = false;
  10.     public bool canPatrol = false;
  11.  
  12.     public PlayerController player;
  13.     public int damage = 1;
  14.     public int awardPoints = 1;
  15.  
  16.     public AudioClip explosionClip;
  17.     AudioSource _audioSource;
  18.     void Start()
  19.     {
  20.         player = GameObject.Find("Player").GetComponent<PlayerController>();
  21.         _audioSource = GameObject.Find("ExplosionSound").GetComponent<AudioSource>();
  22.     }
  23.     void Update()
  24.     {
  25.         if (canPatrol)
  26.         {
  27.             if (!_switch)
  28.             {
  29.                 transform.position = Vector3.MoveTowards(transform.position, pointA.position, speed * Time.deltaTime);
  30.             }
  31.  
  32.             if (_switch)
  33.             {
  34.                 transform.position = Vector3.MoveTowards(transform.position, pointB.position, speed * Time.deltaTime);
  35.             }
  36.  
  37.             if (transform.position == pointA.position)
  38.             {
  39.                 _switch = true;
  40.             }
  41.             if (transform.position == pointB.position)
  42.             {
  43.                 _switch = false;
  44.             }
  45.         }
  46.         else
  47.         {
  48.             transform.Translate(transform.TransformDirection(transform.forward) * speed * Time.deltaTime);
  49.             Destroy(gameObject, 5.0f);
  50.         }
  51.  
  52.     }
  53.  
  54.     private void OnCollisionEnter(Collision collision)
  55.     {
  56.         if(collision.gameObject.tag == "Player")
  57.         {
  58.             player.LoseHealth(damage);
  59.         }
  60.     }
  61.  
  62.     private void OnTriggerEnter(Collider other)
  63.     {
  64.         if(other.gameObject.tag == "Bullet")
  65.         {
  66.             _audioSource.PlayOneShot(explosionClip);
  67.             player.AddPoints(awardPoints);
  68.             Destroy(other.gameObject);
  69.             Destroy(this.gameObject);
  70.  
  71.         }
  72.     }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement