My question perhaps come from my misunderstanding of the RaycastâŚwhy is he looping through each member of the list? The âdistanceâ value upon which he relies on to move is reset each iteration, therefore only the last one is important, so why bother looping through all of them? Are they sorted in reverse order, ie closest contact last? It would still make looping through them pointless, but at least make choosing the last value appear rational.
Iâve not been through that tutorial myself but I can hopefully help a little.
First thing and sorry for being nitpicky but itâs not a raycast, itâs a shape cast. Specifially itâs casting all attached Collider2D on the Rigidbody2D and returning all results. That said, it does return the standard results type of RaycastHit2D which would seem contradictory to what I just said.
All 2D physics queries that return results with distance (RaycastHit2D) are sorted by ascending distance i.e. first result is first hit.
Not being familiar with this tutorial I donât know where this âvelocityâ things is coming from. I donât see it defined. I also donât follow the logic of what itâs trying to achieve.
It may not have been the case when this tutorial was written however now, all 2D physics queries allow results to be passed into a List whose capacity is automatically increased if required. This saves having to reuse oversized arrays.
The use of ârb2d.positionâ is bad as it instantly teleports the body to the new position which also bypasses interpolation and can cause collision tunnelling. It should use Rigidbody2D.MovePosition instread.
1] The logic is used within a 2D sidescroller. The remaining code can be found at the link I posted in the original comment. It is done so that the player can move on slanted surfaces without moving âintoâ the surface. A shape cast (thank for you the distinction!) is cast at the non-modified moving distance and a collision check is performed. The normal from the contact point defines the vector from which the character will move.
2] Yes, I am aware that directly modifying the rigidbodyâs position, or rigidbody.transform.position is bad practice as this avoids proper collision checking. I am surprised both that I didnât notice that, and that this is used within an official Unity tutorialâŚ
But yes, as you said and I thought was most natural ordering, the RaycastHit array stores each object collided within the ray length in ascending order. This makes my original question more puzzling. Why bother looping through EACH object in that array when it is known the first object is the closest?
âWhy bother looping through EACH object in that array when it is known the first object is the closest?â
I did watch that tutorial at some point I think, IIRC it was pretty puzzling how the collision and sliding was created.
If I read the code correctlyâŚ
One reason for looping might be this part, not because of distance but for setting the grounded state:
if (currentNormal.y > minGroundNormalY)
{
grounded = true;
//...
}
Grounded doesnât get reset in each iteration of the loop. So think about a case where character is falling down.
Player is close to the ground and allowed technically to be âgroundedâ based on the distance to ground⌠but the first hit is to a diagonal wall. This wall is too steep to allow player to be grounded. So only the second closest hit will qualify for setting grounded state true.
In this kind of case grounded would be set only when the second closest hitâs normal is checked: