Im making a 2D platformer, but instead of how people normal handle slopes I want the player to rotate to the angle of the slope and move on rotated axis until they leave that slope, like is Sonic. I dont know how to do this though, as the rays from the raycasting arent rotating with the player and there are lots of collision bugs. Any help would greatly appreciated (code in C# please).
If you use Vector3.up, Vector3.forward etc. for your raycasting, then this refers to world space directions. For local space directions, use transform.up, transform.forward etc. These will always follow the object’s rotation.
Your raycasting code is most likely the issue here. If you use transform.TransformDirection() instead of Vector3.down you should get the ray to rotate properly with your player.
For instance:
void FixedUpdate() {
RaycastHit2D hit = Physics2D.Raycast (transform.localPosition, transform.TransformDirection(Vector3.down));
Debug.DrawRay (transform.localPosition, transform.TransformDirection(Vector3.down));
if (hit.collider != null) {
//Your code here
}
}
Good luck with your game!