#a list would be better for allocations
1 messages · Page 1 of 1 (latest)
part of the problem was you were putting stuff in your array even if they were not real hits. then findClosest was not doing any null checks to ensure all hits its looping on are valid
if you used a list, you would only add valid hits to the list that pass the hit.collider != null test
also instead of creating a new list every update you could just do hits.Clear() which would return the lists count back to 0, but keep its capacity so adding to it again will not allocate any new memory
vs the array you were re allocating every frame
i cant test on my system, but this should be how it works using a list instead of array and with a extra check in findClosest so it can not get a index out of range
oh did it not like some of my syntax?
public List<RaycastHit2D> hits = new List<RaycastHit2D>();
if on a older unity or c# can just make line 8 this instead, instead of the shorthand i used
okay so changed the syntax it works but only half of the rays retuen the object
well its not adding any rays that hit nothing
ik im moving the object its hittin into new arrays
new rays*
and then only working if more than 1 ray hits the object
so whats the code actually trying to do?
just cast 150 rays in a circle
and find the closest hit?
yes
cool, i got to go have dinner but when i come back if you are still having issues i will show how i would approach that from scratch
perfect im gonna try to re make it now iv got some new info
it looks like it all should work, i just would not expect all rays to hit something
they dont theres only one object atm
ok will give it a fast try, but question do you need all of the hits or just the closest one
and do you actaully care about the closest hit or just want the closest hit object
i just need to find the closet object, i was using rays so i could simulate some sort of eye sight
cool in a min or 2 should have something pretty simple
that can be made better or worse by tweaking values
also closest based on position
or based on hit
wont matter for most things but could be a important distinction for larger objects
closest to the object that the ray originates from
no i mean the distinction between where the ray hit, and the objects origin point
just need the pos of where the ray hits
as the object that sends the ray will move towards the object it his and collide with it anyway
so....
this.transform.position = Vector3.MoveTowards(transform.position, closestObject.transform.position, 2);
would make the orign of the cast move to the closest object
Ty you are a god
time to go figure out how you did it
yeah it would but would move rather fast
now if you are wanting to do more stuff in update i would give it all its own method that just returns closest
so much has changed since i last tried unity
most of what i have used has been around since i started using unity back in version 4
i taught my self to program i was like 13 and only got the minimum to do what i needed derp
anyway thanks for your help! i need to hit the sack the ray tracing rabbit hole has taken enough energy for today!
there is a bug i just noticed
go on?
the _rayDistance should be the 3rd arg of Raycast
not just making the dir vector longer
raycast does not seem to care about the length of its direction vector
yeah swap the * for a ,
pretty much
i was wondering why it was seeing objects not being touched by the visual line hahaha
also chances are you would want to define a layermask as well
or atleast check your hits, and make sure they are hitting objects you want to follow and not just like a wall or something
yeah i already know how to do that thought im using tags not layers
ill add it in as layers when if it becomes needed, i don't have any plans I'm just experimenting to learn
thanks again peace!
yeah layers would not work now i think about it