- Home /
The question is answered, right answer was accepted
Aligning a sprite to the ground's normal using raycasts
Hello Unity Community!
I am trying to align a sprite to a slope's normal, in a 2D enviroment that uses physics. I am casting four rays from different points situated near the bottom of the sprite to form sort of a round shape (see the screenshot).
Here's some code:
RaycastHit2D ray1=Physics2D.Raycast(anchor1.gameObject.transform.position,-Vector2.up,0.25f,whatIsGround);
RaycastHit2D ray2=Physics2D.Raycast(anchor2.gameObject.transform.position,-Vector2.up,0.25f,whatIsGround);
RaycastHit2D ray3=Physics2D.Raycast(anchor3.gameObject.transform.position,-Vector2.up,0.25f,whatIsGround);
RaycastHit2D ray4=Physics2D.Raycast(anchor4.gameObject.transform.position,-Vector2.up,0.25f,whatIsGround);
this.transform.up=(ray1.normal + ray2.normal + ray3.normal + ray4.normal )/4;
This happens in Update(), and works well most of the time. However, sometimes, if there's a sharp angle and I stop the character right in that spot, the sprite literally has seizures.
So, would more rays help to smooth it out? Should they be focused more towards the center and not spread out? How could I prevent the constant jumbling?
Thanks for your time!
Answer by Ashky · Oct 09, 2015 at 09:58 PM
If anyone is interested, using more rays smooths it out and doesn't hit performance at all. I ended up using 8 rays and avoiding terrain with sharp angles under 90 degrees.