How do I get closest particle?
Hello, I am currently working on a small project and I am currently stuck as I don't know how to find closest particle. The thing I want to achive here is that enemies will dodge the particles (bullets). I have already found this (source) :
// Find the name of the closest enemy
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public GameObject FindClosestEnemy()
{
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("Enemy");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
}
but I have no idea how to make it work with particles.
Thank you for all answers and ideas :) .
Answer by ifurkend · May 01, 2018 at 01:47 AM
Use GetParticles to get the positions into another Vector3 array. You can refer to the sample in the script manual without the SetParticles line because you’re not changing the behavior of the emitted particles.
Your answer
![](https://koobas.hobune.stream/wayback/20220612161800im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Quickly find all particles on the scene without using GetComponent 1 Answer
Issues with dropping an object on multiple objects separetly - Simple 2d Drag & Drop 0 Answers
Need some assistance with spot/point lighting for GPU based particles 0 Answers
how to stop particles shrink texture 1 Answer
Shuriken :: flip texture in Stretched Billboard mode 0 Answers