Hi,
So basically I have a raycast that collects the Vector3 position of its collision and stores that in the current slot of an array.
I want it to scan a massive rectangle 2000 times length ways and 1000 height. I have done this in an if statement and it takes SOO long to complete but doesn’t crash my computer.
I have done it as a massive for loop and that crashes my computer.
I have done it as a combination where it goes along using if statement and collects like 100 points and that still has performance issues.
What I’m asking is this just something that will take ages to do due to the large amount of data being collected or is there a much much easier way to do this?
Sorry I am not amazing with unity. Using c#.
Thanks in advance
Well, you’re trying to do 2 million raycasts there, and that won’t be quick.
A few thoughts:
- Why are you raycasting (and by that I mean what are you trying to achieve)? If it’s just against a rectangle and you want to see if a point is inside or outside of that rectangle then there are much faster ways to do this (including some built in methods: Unity - Scripting API: Rect.Contains )
- If you absolutely must raycast and store the resultant array, then make sure your initialising the size of the array correctly. For example, if your using List then every time you add a new Vector3 to that list, you might end up having to do a lot of memory allocation with each additional add, which will get worse over time. Reserving the list size will help with this although storing and later iterating over 2milliion results seems very inefficient ( for reference, you can initialise list size to 10 elements with List myList = new List(10) ).