How to rotate a KinematicBody to align with floor normal like in a Sonic game. (3D)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Calebunga

Hello, I’d want to start by saying that I am pretty new to godot as well as scripting in general so hopefully you’ll bear with me…

I am trying to make a 3D sonic fan game in Godot and I’m currently trying to figure out how to have a Kinematic Body handle slopes. The idea is to make it’s rotation match whatever surface it is standing on.

2D Example1
2D Example2

I’ve done my fair share of research on how it works in 2D thanks to the Sonic Physics Guide, as well as on linear algebra, dot and cross products, and quaternions. My problem now is simply translating all of this into a 3D Godot project.

I have a Raycast pointing down relative to the player and I have this script attached to it:

extends RayCast

func _physics_process(delta):

#Adjust the normal using the collision point
var RelativeNormal = \
get_node("..").transform.origin + self.get_collision_normal();
#Use the godot look_at function to rotate the object
get_node("..").look_at(RelativeNormal, Vector3.UP)

There are some problems, though…

First of all, it isn’t rotated correctly. It’s on it’s back and I think it’s tilted a little.

Secondly, after thinking it through, I realized that even if I get it to align to the ground I will also need to rotate the character based on player movement inputs and where the camera is looking. I’ve seen many tutorials on that, but putting that together with slope physics adds another layer to the issue.

I have asked questions similar to this at r/godot and on the discord server and I’ve gotten some good advice. I find that the more I ask, although I’m not getting one full solution (for obvious reasons), those that have responded have given me more and more pieces of good and useful advice. That being said, I welcome any input and thoughts on the matter. Thank you! =)