- Home /
Question by
Mi37ke · Feb 06, 2018 at 04:03 PM ·
2d-platformeralign
How to align a 2D sprite to the surface it is touching
hello, i currently have a working version of code to align a sprite to the ground, but I would also like to give my character the ability to hang from the ceiling. But i cannot figure out how alter the code to get this functionality. I am fairly sure that colliding with the ceiling box collider is causing my sprites rotation to change and gradually rotates the sprite till is upside down and falls off the ceiling. i know freezing the Z rotation will fix gradually rotation problem. my plan is to snap the sprites rotation to the ceilings rotation once it enters the ceilings trigger, then freeze the Z rotation. so my question is how can I snap my sprites rotation to the ceilings rotations? here is my code for adjusting to the grounds rotation
const float standardRotation = -90f;
const float rayDistance = 1f;
float angle = standardRotation;
void FixedUpdate()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, -transform.up, rayDistance);
rbControl.rotation = Mathf.Atan2(hit.normal.y, hit.normal.x) * Mathf.Rad2Deg + standardRotation;
}
Comment