Advertisement
lemansky

Untitled

Apr 4th, 2021
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 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.  
  11.     public bool canPatrol = false;
  12.  
  13.     public int damage = 1;
  14.     public int awardPoints = 1;
  15.  
  16.     public PlayerController player;
  17.     void Start()
  18.     {
  19.         player = GameObject.Find("Player").GetComponent<PlayerController>();
  20.     }
  21.  
  22.     // Update is called once per frame
  23.     void Update()
  24.     {   if (canPatrol)
  25.         {
  26.             if (!_switch)
  27.             {
  28.                 transform.position = Vector3.MoveTowards(transform.position, pointA.position, speed * Time.deltaTime);
  29.             }
  30.  
  31.             if (_switch)
  32.             {
  33.                 transform.position = Vector3.MoveTowards(transform.position, pointB.position, speed * Time.deltaTime);
  34.             }
  35.  
  36.             if (transform.position == pointA.position)
  37.             {
  38.                 _switch = true;
  39.             }
  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.     private void OnCollisionEnter(Collision collision)
  54.     {
  55.         if (collision.gameObject.tag == "Player")
  56.         {
  57.             player.LoseHealth(damage);
  58.         }
  59.     }
  60.  
  61.     private void OnTriggerEnter(Collider other)
  62.     {
  63.         if (other.gameObject.tag == "Bullet")
  64.         {
  65.             player.AddPoints(awardPoints);
  66.             Destroy(other.gameObject);
  67.             Destroy(this.gameObject);
  68.         }
  69.     }
  70.  
  71.  
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement