- Home /
Detect slope with raycast2d
How can i shoot a normal ray-cast2d in a direction with a variable set to the length and get the angle/slope of the point it hits (it will probably hit a sprite-shape). And if it hits nothing output the angle as something above 360
Answer by BetaFruit · Jan 05 at 05:34 PM
I have figured it out! I had seen this post https://answers.unity.com/questions/1038928/how-to-detect-slope-angle-in-2d.html when I posted this question but I couldn't get it working how I wanted it to. here is my solution:
RaycastHit2D[] hits = new RaycastHit2D[2]; int h = Physics2D.RaycastNonAlloc(transform.position, Vector2.down, hits, checkDistance); //____________________________________________________________________________________________ if (h > 1) { downangle = Mathf.Atan2(hits[1].normal.x, hits[1].normal.y) * Mathf.Rad2Deg; } //____________________________________________________________________________________________ else { downangle = 1000; }
in order to change the raycast angle you can replace vector2.down with a new vector2 with the angles you want.