#a list would be better for allocations

1 messages · Page 1 of 1 (latest)

wheat sphinx
#

im so lost so much info was just thrown at me

slender loom
#

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

wheat sphinx
#

hang on i need to update vs

#

im on 8.0 not 9.0

slender loom
#

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

wheat sphinx
#

okay so changed the syntax it works but only half of the rays retuen the object

slender loom
#

well its not adding any rays that hit nothing

wheat sphinx
#

ik im moving the object its hittin into new arrays

#

new rays*

#

and then only working if more than 1 ray hits the object

slender loom
#

so whats the code actually trying to do?

#

just cast 150 rays in a circle

#

and find the closest hit?

wheat sphinx
#

yes

slender loom
#

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

wheat sphinx
#

perfect im gonna try to re make it now iv got some new info

slender loom
#

it looks like it all should work, i just would not expect all rays to hit something

wheat sphinx
#

they dont theres only one object atm

slender loom
#

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

wheat sphinx
#

i just need to find the closet object, i was using rays so i could simulate some sort of eye sight

slender loom
#

cool in a min or 2 should have something pretty simple

wheat sphinx
#

that can be made better or worse by tweaking values

slender loom
#

also closest based on position

#

or based on hit

#

wont matter for most things but could be a important distinction for larger objects

wheat sphinx
#

closest to the object that the ray originates from

slender loom
#

no i mean the distinction between where the ray hit, and the objects origin point

wheat sphinx
#

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

slender loom
#

is how i would approach it

wheat sphinx
#

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

slender loom
#

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

wheat sphinx
#

so much has changed since i last tried unity

slender loom
#

most of what i have used has been around since i started using unity back in version 4

wheat sphinx
#

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!

slender loom
#

there is a bug i just noticed

wheat sphinx
#

go on?

slender loom
#

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

wheat sphinx
#

yeah swap the * for a ,

slender loom
#

pretty much

wheat sphinx
#

i was wondering why it was seeing objects not being touched by the visual line hahaha

slender loom
#

yeah that is why

#

3rd arg of raycast defines the max distance

wheat sphinx
#

fixed it anyway

#

thanks again!

slender loom
#

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

wheat sphinx
#

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!

slender loom
#

yeah layers would not work now i think about it