Комментарии:
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.
Ответитьhow tf you add new component RandomWalk and why there is no script for that? I can't use the kdTree without this component
ОтветитьBrilliant! Thank you very much for this =)
Ответитьcan you show us the random walk script ?
ОтветитьYou don't use Vector3.Distance to find the closest object, it's too expensive
ОтветитьWhat a waste of good 4 minutes :)
Ответить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
Ответитьwish you had recorded this in a resolution where i could actually see the difference between = and -
ОтветитьБлагодарю😘
ОтветитьDwarf Fortress background music vibes
ОтветитьThank you <3
Ответить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 ;
}
cnt see the code
ОтветитьThank you for posting the code!
Ответить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
Ответить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);
}
you saved me probably a weeks worth of trouble man, thank you so much.
Ответить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.
I've quickly implemented this into my 3rd person shooter. You are a GOD amongst men! It works flawelessly!
ОтветитьThank you! This is very useful. I may be 4 years too late but exactly what I'm looking for. Thank you!
Ответить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!!
Awesome tutorial! I learned about kd trees in school and always wondered how I could use them in unity. Excited to try this out
ОтветитьI would need the closest 4 objects in a 3D space?
Ответитьtoo blurry I can't read the code.
Ответитьyeah but how does the kd tree work?
Ответить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
ОтветитьAs much as I would like to watch this, that white ide is blinding.
Ответитьinstead of looping thorugh all the balls, each ball could just use a sphere cast to find the ones nearest and loop thorugh those
Ответить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
Ответить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!
thnx
Ответитьwhat if I wanted to find the closest 3?
ОтветитьDo you have a version to find the furthest object?
ОтветитьHi, where can I download the full balls example? Thanks
Ответитьyou speak game zlap.ip ?
Ответитьrichtiger deutscher "null" anstatt einfach sero
ОтветитьVery useful for my rts.
ОтветитьI'm curious how much of the frame drop was from the gizmo line drawing.
Ответить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?
What's in "RandomWalk" ? I can't get my head around with templates/lists.
ОтветитьWhat if a unit gets destroyed? Cant remove object from list while iterating no?
Ответить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);
}
is it possible to find the seccond closest object with kdTree?
ОтветитьYou did not expain
Ответитьmusic?
Ответитьvery inefficient to do this better use sphere overlap
ОтветитьIs it possible for you to show me your script in RandomWalk.cs? Thanks!
ОтветитьAny advice on how to use this with the jobs system?
ОтветитьThis is awesome!!! thank you for sharing :)
ОтветитьHow would you go about modifying your code so that objects within the same tree can search for their nearest neighbor without finding themselves?
Ответить