Скрипт Monster
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Monster : Unit
{
protected virtual void Awake(){ }
protected virtual void Start(){ }
protected virtual void Update(){ }
protected virtual void OnTriggerEnter2D(Collider2D collider)
{
Bullet bullet = collider.GetComponent<Bullet>();
if (bullet)
{
ReceiveDamage();
}
Hero hero = collider.GetComponent<Hero>();
if (hero)
{
hero.ReceiveDamage();
}
}
}
Скрипт MoveableMonster
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class MoveableMonster : Monster
{
[SerializeField]
private float speed = 2.0F;
private Bullet bullet;
private Vector3 direction;
private SpriteRenderer sprite;
protected override void Awake()
{
sprite = GetComponentInChildren<SpriteRenderer>();
bullet = Resources.Load<Bullet>("Bullet");
}
protected override void Start()
{
direction = transform.right;
}
protected override void Update()
{
Move();
}
protected override void OnTriggerEnter2D(Collider2D collider)
{
Unit unit = collider.GetComponent<Unit>();
if (unit && unit is Hero)
{
if (Mathf.Abs(unit.transform.position.x - transform.position.x)< 0.3F) ReceiveDamage();
else unit.ReceiveDamage();
}
}
private void Move()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position + transform.up * 0.5F + transform.right * direction.x * 0.5F, 0.1F);
if (colliders.Length > 0 && colliders.All(x=> !x.GetComponent<Hero>())) direction *= -1.0F;
transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime);
}
}