i am making a game partially inspired by geometry dash. as the character is a geometric shape, i need it to rotate at the same angle as the surface. i have some code relying on raycasthit2d.normal for that but there is one problem. collider.bounds is pretty useless for this as it gets affected by rotation. this causes some offsets which are super annoying. this means i get weird bugs like this(the white line is the raycast and extra height is 0.05):
my code is 100% done except for this annoying bug. my code:
private void Rotate()
{
RaycastHit2D raycastHit;
Quaternion origRot = transform.rotation;
float raycastHeight = boxCollider.bounds.extents.y + extraRotationRaycastHeight;
// i added this line which sets the rotation to the previous rotation due to the fact that i use quaternion.rotatetowards for smooth rotation
transform.rotation = rotation;
if (isFacingRight)
{
raycastHit = Physics2D.Raycast(new Vector2(boxCollider.bounds.center.x + boxCollider.bounds.extents.x, boxCollider.bounds.center.y), -transform.up, raycastHeight, groundLayer);
Debug.DrawRay(new Vector2(boxCollider.bounds.center.x + boxCollider.bounds.extents.x, boxCollider.bounds.center.y), -raycastHeight * transform.up);
}
else
{
raycastHit = Physics2D.Raycast(new Vector2(boxCollider.bounds.center.x - boxCollider.bounds.extents.x, boxCollider.bounds.center.y), -transform.up, raycastHeight, groundLayer);
Debug.DrawRay(new Vector2(boxCollider.bounds.center.x - boxCollider.bounds.extents.x, boxCollider.bounds.center.y), -raycastHeight * transform.up);
}
transform.rotation = origRot;
rotation = Quaternion.FromToRotation(Vector3.up, raycastHit.normal);
rotation.x = 0;
rotation.y = 0;
}
the part of the code in fixedupdate that actually rotates the object:
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation, rotationSpeed * Time.fixedDeltaTime);
its in fixedupdate so that it happens on the same clock as my movement.
this code is most likely trash so if you can help fix it
thanks in advance for any help with this annoying problem