So, I have this very strange bug.
I have a particle system flamethrower and it kills enemies using OnPatricleCollision().
So the problem is, that the flamethrower just stops sending collision messages and stops killing as the colliders disappeared on enemies since the particles still collide with walls.
here’s the enemy code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class enemyAI : MonoBehaviour
{
private Animator anim;
[SerializeField] GameObject cudoriste;
private Rigidbody rg;
[SerializeField] private GameObject Fireball;
public NavMeshAgent agent;
public Transform player;
public LayerMask whatIsGround, whatIsPlayer;
//Patrolling
public Vector3 walkPoint;
bool walkPointSet;
public float walkPointRange;
//Attack
public float timeBetweenAttacks;
bool alreadyAttacked;
//Sates
public float sightRange, attackRange;
public bool playerInSightRange, playerInAttackRange;
private void Awake()
{
player = GameObject.Find("Player").transform;
agent = GetComponent<NavMeshAgent>();
anim = cudoriste.GetComponent<Animator>();
anim.Play("Idle");
rg = GetComponent<Rigidbody>();
}
private void Update()
{
//Can he see him and does he attack
playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
if (!playerInSightRange && !playerInAttackRange)
{
Patrolling();
}
if (playerInSightRange && playerInAttackRange)
{
AttackPlayer();
}
if (playerInSightRange && !playerInAttackRange)
{
ChasePlayer();
}
}
private void Patrolling()
{
anim.Play("Walk");
if (!walkPointSet)
SearchWalkPoint();
if (walkPointSet)
agent.SetDestination(walkPoint);
Vector3 distanceToWalkPoint = transform.position - walkPoint;
//Walkpoint reached
if (distanceToWalkPoint.magnitude < 1f)
walkPointSet = false;
}
private void SearchWalkPoint()
{
//Calculate random point in range
float randomZ = Random.Range(-walkPointRange, walkPointRange);
float randomX = Random.Range(-walkPointRange, walkPointRange);
walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
walkPointSet = true;
}
private void ChasePlayer()
{
anim.Play("Walk");
agent.SetDestination(player.position);
}
private void AttackPlayer()
{
anim.Play("Idle");
//Zli cika ne sme da se krece
agent.SetDestination(transform.position);
Vector3 targetPostition = new Vector3(player.position.x, this.transform.position.y, player.position.z);
this.transform.LookAt(targetPostition);
if (!alreadyAttacked)
{
Rigidbody rb = Instantiate(Fireball, transform.position + new Vector3(0,4,0), transform.rotation * Quaternion.Euler(transform.position.y - player.position.y, 0 ,0)).GetComponent<Rigidbody>();
alreadyAttacked = true;
Invoke(nameof(ResetAttack), timeBetweenAttacks);
}
}
private void ResetAttack()
{
alreadyAttacked = false;
}
void OnParticleCollision(GameObject other)
{
if (other.transform.tag == "Weapon")
{
Destroy(gameObject);
}
}
}