lemansky

Untitled

Apr 11th, 2021 (edited)
1,169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 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.         audioSource.clip = explosionClip;
  23.     }
  24.     void Update()
  25.     {
  26.         if (canPatrol)
  27.         {
  28.             if (!_switch)
  29.             {
  30.                 transform.position = Vector3.MoveTowards(transform.position, pointA.position, speed * Time.deltaTime);
  31.             }
  32.  
  33.             if (_switch)
  34.             {
  35.                 transform.position = Vector3.MoveTowards(transform.position, pointB.position, speed * Time.deltaTime);
  36.             }
  37.  
  38.             if (transform.position == pointA.position)
  39.             {
  40.                 _switch = true;
  41.             }
  42.             if (transform.position == pointB.position)
  43.             {
  44.                 _switch = false;
  45.             }
  46.         }
  47.         else
  48.         {
  49.             transform.Translate(transform.TransformDirection(transform.forward) * speed * Time.deltaTime);
  50.             Destroy(gameObject, 5.0f);
  51.         }
  52.  
  53.     }
  54.  
  55.     private void OnCollisionEnter(Collision collision)
  56.     {
  57.         if(collision.gameObject.tag == "Player")
  58.         {
  59.             player.LoseHealth(damage);
  60.         }
  61.     }
  62.  
  63.     private void OnTriggerEnter(Collider other)
  64.     {
  65.         if(other.gameObject.tag == "Bullet")
  66.         {
  67.             _audioSource.Play();
  68.             player.AddPoints(awardPoints);
  69.             Destroy(other.gameObject);
  70.             Destroy(this.gameObject);
  71.  
  72.         }
  73.     }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment