Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Enemy : MonoBehaviour
- {
- public Transform pointA = null, pointB = null;
- public float speed = 3.0f;
- bool _switch = false;
- public bool canPatrol = false;
- public PlayerController player;
- public int damage = 1;
- public int awardPoints = 1;
- public AudioClip explosionClip;
- AudioSource _audioSource;
- void Start()
- {
- player = GameObject.Find("Player").GetComponent<PlayerController>();
- _audioSource = GameObject.Find("ExplosionSound").GetComponent<AudioSource>();
- }
- void Update()
- {
- if (canPatrol)
- {
- if (!_switch)
- {
- transform.position = Vector3.MoveTowards(transform.position, pointA.position, speed * Time.deltaTime);
- }
- if (_switch)
- {
- transform.position = Vector3.MoveTowards(transform.position, pointB.position, speed * Time.deltaTime);
- }
- if (transform.position == pointA.position)
- {
- _switch = true;
- }
- if (transform.position == pointB.position)
- {
- _switch = false;
- }
- }
- else
- {
- transform.Translate(transform.TransformDirection(transform.forward) * speed * Time.deltaTime);
- Destroy(gameObject, 5.0f);
- }
- }
- private void OnCollisionEnter(Collision collision)
- {
- if(collision.gameObject.tag == "Player")
- {
- player.LoseHealth(damage);
- }
- }
- private void OnTriggerEnter(Collider other)
- {
- if(other.gameObject.tag == "Bullet")
- {
- _audioSource.PlayOneShot(explosionClip);
- player.AddPoints(awardPoints);
- Destroy(other.gameObject);
- Destroy(this.gameObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement