Unity Trick: Find closest Object fast! (k-d Tree)

Unity Trick: Find closest Object fast! (k-d Tree)

DitzelGames

6 лет назад

57,869 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

SpeedCoder2000
SpeedCoder2000 - 23.07.2023 00:00

Hi. Thanks for your amazing script. Is very useful, but i have a question. ¿Is there a way to get the 'N' nearest objects to a point?. In my case at this moment i need to get the two nearest objects to the player's position.

Ответить
jacob zak
jacob zak - 05.05.2023 20:51

how tf you add new component RandomWalk and why there is no script for that? I can't use the kdTree without this component

Ответить
Johnny Thousand
Johnny Thousand - 22.04.2023 07:25

Brilliant! Thank you very much for this =)

Ответить
Nεκτάριος Μπούμπαλος
Nεκτάριος Μπούμπαλος - 03.03.2023 16:39

can you show us the random walk script ?

Ответить
The soul of a mortal
The soul of a mortal - 06.02.2023 06:05

You don't use Vector3.Distance to find the closest object, it's too expensive

Ответить
The soul of a mortal
The soul of a mortal - 06.02.2023 06:03

What a waste of good 4 minutes :)

Ответить
Greg Eckermann
Greg Eckermann - 22.01.2023 07:32

I might try to convert the script to Jobsystem for more faster speed. but to be blunt you should instance the balls would show it's try potential

Ответить
White Tiana
White Tiana - 12.01.2023 19:45

wish you had recorded this in a resolution where i could actually see the difference between = and -

Ответить
Rimuru Dev
Rimuru Dev - 19.11.2022 12:01

Благодарю😘

Ответить
Shasaur
Shasaur - 08.10.2022 13:31

Dwarf Fortress background music vibes

Ответить
Adis
Adis - 06.09.2022 15:16

Thank you <3

Ответить
Glitch Over
Glitch Over - 06.08.2022 14:42

Transform GetClosest(Transform[] arrayOfObject)
{
Transform _transform = null;
float minDist = Mathf.Infinity;
Vector3 currentPos = transform.position;
foreach (Transform t in arrayOfObject)
{
float dist = Vector3.Distance(t.position, currentPos);
if (dist < minDist)
{
_transform = t;
minDist = dist;
}
}
return _transform ;
}

Ответить
GD_Helper
GD_Helper - 27.06.2022 12:26

cnt see the code

Ответить
LankyAdder
LankyAdder - 02.06.2022 19:01

Thank you for posting the code!

Ответить
Suleiman Abdullah
Suleiman Abdullah - 13.05.2022 18:06

can you explain the first approach why most of people use MathF.Infinity/float.MaxValue am wondering how this is assigned to the closet distance when distance is less than Mathf.Infinity/float.MaxValue

Ответить
Mas Jawa
Mas Jawa - 14.04.2022 02:29

Thankyou, may God bless you my friend... 🤩🤩🤩

If someone need to remove spesific item in list (maybe like enemy dead, so it shouldn't be included in list anymore). You can do such this..

public void removeFromList(EnemyData enemyData) {
int position = allEnemies.ToList().IndexOf(enemyData);
allEnemies.RemoveAt(position);
}

Ответить
S. C
S. C - 13.04.2022 21:17

you saved me probably a weeks worth of trouble man, thank you so much.

Ответить
Tony
Tony - 27.03.2022 23:08

Heads up, this is actually a slower method cause you're calling distance twice: one for the IF and one for the VALUE.

Save it to a variable before the if statement and reuse the variable. It will cut your calculations in half. Distance is expensive cause it uses roots.

Ответить
Tone IT
Tone IT - 06.03.2022 21:40

I've quickly implemented this into my 3rd person shooter. You are a GOD amongst men! It works flawelessly!

Ответить
rcdemoral1982
rcdemoral1982 - 18.02.2022 22:21

Thank you! This is very useful. I may be 4 years too late but exactly what I'm looking for. Thank you!

Ответить
MaruTheMonkey
MaruTheMonkey - 15.02.2022 18:41

Hi Ditzel, as a duck-tape-and-hatchet-kinda amateur programmer, there are a few basic things I do not understand about this approach, before even diving into the code.

As I understand it, data structured in a k-d tree facilitates the search for an element by reducing the (maximum) number of queries to the number of child nodes going from (and including) the root node, instead of querying every single element. >> just talking about a single dimension now << The underlying scheme being one "big" computational effort to structure the data, then a lot of "small" computational efforts for the search. This lends itself to use-cases that require a lot of queries on the same set of data. E.g. comparing two point clouds A and B, to find the closest a in A to each b in B, where you don't want to query every element individually, resulting in n(a)^n(b) queries.

However, the use-case described in this video basically needs a lot of single queries on a set of data that changes with every query. Hence, you have to restructure the data with every query. How is that more performant than a single vector comparison looping through every element. I just don't see the use-case here. I want to be really clear btw. that I am NOT saying there isn't one and this is in no way criticism. I just don't understand it.

Auf jeden Fall auch Danke für die lehrreichen Videos!!

Ответить
Balloon Moose
Balloon Moose - 23.12.2021 21:48

Awesome tutorial! I learned about kd trees in school and always wondered how I could use them in unity. Excited to try this out

Ответить
Edward Dewolf
Edward Dewolf - 12.12.2021 13:05

I would need the closest 4 objects in a 3D space?

Ответить
Fifi Lee
Fifi Lee - 16.11.2021 02:05

too blurry I can't read the code.

Ответить
SeppuKun
SeppuKun - 31.07.2021 00:21

yeah but how does the kd tree work?

Ответить
Kumsu
Kumsu - 30.04.2021 16:00

thanks for the code really helpful And Plz add find an index to the Code. I see remove at in the code. but missing indexOF

Ответить
MostHated
MostHated - 29.04.2021 03:27

As much as I would like to watch this, that white ide is blinding.

Ответить
Linton FOR
Linton FOR - 23.01.2021 13:38

instead of looping thorugh all the balls, each ball could just use a sphere cast to find the ones nearest and loop thorugh those

Ответить
Loniek Frozenn
Loniek Frozenn - 23.11.2020 14:03

Hey, i'm having a problem, if doesn't find closest but first in list object. I have all objects in list set activa false and they appear in time and gets active true and the closest function just look for the newest set active true

Ответить
Wii U
Wii U - 18.10.2020 05:05

I get this error:
unity The type 'system' cannot be used as type parameter 'T' in the generic type or method 'KdTree<T>'. There is no implicit reference conversion from 'system' to 'UnityEngine.Component'.

when using this code:
protected KdTree<system> systems = new KdTree<system>(); is wrong

The basics of what I am doing is enabling a model solar system when the player gets close to it and disabling the rest of the solar systems. Please help.

Thank You!

Ответить
Jahn Star
Jahn Star - 03.09.2020 02:05

thnx

Ответить
Jesper
Jesper - 20.08.2020 01:15

what if I wanted to find the closest 3?

Ответить
Solal Coqueron
Solal Coqueron - 06.07.2020 16:51

Do you have a version to find the furthest object?

Ответить
Rubén Peinado
Rubén Peinado - 01.07.2020 17:46

Hi, where can I download the full balls example? Thanks

Ответить
Walney moreira klein
Walney moreira klein - 25.04.2020 07:50

you speak game zlap.ip ?

Ответить
nebel nice
nebel nice - 21.04.2020 16:04

richtiger deutscher "null" anstatt einfach sero

Ответить
sammy sammy
sammy sammy - 11.03.2020 23:38

Very useful for my rts.

Ответить
K
K - 08.02.2020 10:02

I'm curious how much of the frame drop was from the gizmo line drawing.

Ответить
Gentleman
Gentleman - 13.01.2020 03:59

Hi Ditzel,
Is your code CC0, or what license would that be?
Essentially can I just take it and use it in an open source project? If yes, do I need to credit you somehow?

Ответить
Little Arvin
Little Arvin - 02.01.2020 18:04

What's in "RandomWalk" ? I can't get my head around with templates/lists.

Ответить
Maths Plus Games
Maths Plus Games - 02.11.2019 21:44

What if a unit gets destroyed? Cant remove object from list while iterating no?

Ответить
Auditors United
Auditors United - 06.10.2019 13:25

using System.Linq;
GameObject GetNearestTarget()
{
//so lets say you want the closest target from a array (in this case all Gameobjects with Tag "enemy") and let's assume this script right now is on the player (or the object with which the distance has to be calculated)
return GameObject.FindGameObjectsWithTag("deer").Aggregate((o1, o2) => Vector3.Distance(o1.transform.position, this.transform.position) > Vector3.Distance(o2.transform.position, this.transform.position) ? o2 : o1);
}

Ответить
Joseph P
Joseph P - 04.07.2019 23:25

is it possible to find the seccond closest object with kdTree?

Ответить
Tomkr T
Tomkr T - 17.06.2019 11:32

You did not expain

Ответить
Bilolado
Bilolado - 14.04.2019 05:49

music?

Ответить
snake555510
snake555510 - 24.02.2019 20:36

very inefficient to do this better use sphere overlap

Ответить
Olivier Beauchemin
Olivier Beauchemin - 25.09.2018 22:00

Is it possible for you to show me your script in RandomWalk.cs? Thanks!

Ответить
Abd ElRahman Mohammed
Abd ElRahman Mohammed - 25.08.2018 20:15

Any advice on how to use this with the jobs system?

Ответить
Eric Daily
Eric Daily - 22.08.2018 09:26

This is awesome!!! thank you for sharing :)

Ответить
Pirate Skeleton
Pirate Skeleton - 26.06.2018 05:13

How would you go about modifying your code so that objects within the same tree can search for their nearest neighbor without finding themselves?

Ответить