Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Particle system collision stops working

Discussion in 'Physics' started by Matke9, Dec 22, 2020.

  1. Matke9

    Matke9

    Joined:
    Nov 20, 2020
    Posts:
    2
    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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class enemyAI : MonoBehaviour
    7. {
    8.     private Animator anim;
    9.     [SerializeField] GameObject cudoriste;
    10.     private Rigidbody rg;
    11.  
    12.     [SerializeField] private GameObject Fireball;
    13.  
    14.     public NavMeshAgent agent;
    15.  
    16.     public Transform player;
    17.  
    18.     public LayerMask whatIsGround, whatIsPlayer;
    19.  
    20.     //Patrolling
    21.     public Vector3 walkPoint;
    22.     bool walkPointSet;
    23.     public float walkPointRange;
    24.  
    25.     //Attack
    26.     public float timeBetweenAttacks;
    27.     bool alreadyAttacked;
    28.  
    29.     //Sates
    30.     public float sightRange, attackRange;
    31.     public bool playerInSightRange, playerInAttackRange;
    32.  
    33.     private void Awake()
    34.     {
    35.         player = GameObject.Find("Player").transform;
    36.         agent = GetComponent<NavMeshAgent>();
    37.         anim = cudoriste.GetComponent<Animator>();
    38.         anim.Play("Idle");
    39.         rg = GetComponent<Rigidbody>();
    40.     }
    41.  
    42.     private void Update()
    43.     {
    44.         //Can he see him and does he attack
    45.         playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
    46.         playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
    47.  
    48.         if (!playerInSightRange && !playerInAttackRange)
    49.         {
    50.             Patrolling();
    51.         }
    52.         if (playerInSightRange && playerInAttackRange)
    53.         {
    54.             AttackPlayer();
    55.         }
    56.         if (playerInSightRange && !playerInAttackRange)
    57.         {
    58.             ChasePlayer();
    59.         }
    60.  
    61.         }
    62.  
    63.     private void Patrolling()
    64.     {
    65.         anim.Play("Walk");
    66.         if (!walkPointSet)
    67.             SearchWalkPoint();
    68.         if (walkPointSet)
    69.             agent.SetDestination(walkPoint);
    70.  
    71.         Vector3 distanceToWalkPoint = transform.position - walkPoint;
    72.  
    73.         //Walkpoint reached
    74.         if (distanceToWalkPoint.magnitude < 1f)
    75.             walkPointSet = false;
    76.     }
    77.  
    78.     private void SearchWalkPoint()
    79.     {
    80.         //Calculate random point in range
    81.         float randomZ = Random.Range(-walkPointRange, walkPointRange);
    82.         float randomX = Random.Range(-walkPointRange, walkPointRange);
    83.  
    84.         walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
    85.  
    86.         if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
    87.             walkPointSet = true;
    88.     }
    89.  
    90.     private void ChasePlayer()
    91.     {
    92.         anim.Play("Walk");
    93.         agent.SetDestination(player.position);
    94.     }
    95.  
    96.     private void AttackPlayer()
    97.     {
    98.         anim.Play("Idle");
    99.  
    100.         //Zli cika ne sme da se krece
    101.         agent.SetDestination(transform.position);
    102.  
    103.         Vector3 targetPostition = new Vector3(player.position.x, this.transform.position.y, player.position.z);
    104.         this.transform.LookAt(targetPostition);
    105.  
    106.         if (!alreadyAttacked)
    107.         {
    108.             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>();
    109.  
    110.             alreadyAttacked = true;
    111.             Invoke(nameof(ResetAttack), timeBetweenAttacks);
    112.         }
    113.     }
    114.  
    115.     private void ResetAttack()
    116.     {
    117.         alreadyAttacked = false;
    118.     }
    119.     void OnParticleCollision(GameObject other)
    120.     {
    121.         if (other.transform.tag == "Weapon")
    122.         {
    123.             Destroy(gameObject);
    124.         }
    125.     }
    126. }
    127.  
     
  2. Matke9

    Matke9

    Joined:
    Nov 20, 2020
    Posts:
    2
    Ok, I found out what was causing problems, it was the navMeshAgent component