Здравствуйте, создал скрипт пули и для стреляющих врагов, но выстрел происходит как по игроку так и по своим. как можно реализовать чтобы враги не убивали своих?
Код на пули
Код
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
private GameObject parent;
public GameObject Parent { set { parent = value; } }
private float speed = 30.0F;
private Vector3 direction;
public Vector3 Direction { set { direction = value; } }
public float shootingRate = 0.25f;
private float shootCooldown;
private SpriteRenderer sprite;
private void Awake()
{
sprite = GetComponentInChildren<SpriteRenderer>();
}
private void Start()
{
shootCooldown = 0f;
{
shootCooldown = 0f;
}
Destroy(gameObject, 0.5F);
}
private void OnTriggerEnter2D(Collider2D collider)
{
if (collider.tag == "Ground")
{
Destroy(gameObject);
}
{
Unit unit = collider.GetComponent<Unit>();
if (unit && unit.gameObject != parent)
{
unit.ReceiveDamage();
Destroy(gameObject);
}
}
}
private void Update()
{
transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime);
if (shootCooldown > 0)
{
shootCooldown -= Time.deltaTime;
}
}
}
Код на стреляющего врага
Код
sing System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShootableMonster : Monster
{
[SerializeField]
private float rate = 2.0F;
private Bullet bullet;
protected override void Awake()
{
bullet = Resources.Load<Bullet>("Bullet");
}
protected override void Start()
{
InvokeRepeating("Shoot", rate, rate);
}
private void Shoot()
{
Vector3 position = transform.position;
position.y += 0.4F;
Bullet newBullet = Instantiate(bullet, position, bullet.transform.rotation) as Bullet;
newBullet.Parent = gameObject;
newBullet.Direction = -newBullet.transform.right;
Collider2D collider = GetComponent<Collider2D>();
if (collider.tag == "player")
{
ReceiveDamage();
}
}
protected override void OnTriggerEnter2D(Collider2D collider)
{
Unit unit = collider.GetComponent<Unit>();
if (unit && unit is Character)
{
if (Mathf.Abs(unit.transform.position.x - transform.position.x) < 0.3F) ReceiveDamage();
else unit.ReceiveDamage();
}
}
}