How to make the sprite rotate to be relative to the angle of the ground coalition?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Noob_Maker
:warning: Old Version Published before Godot 3 was released.

So, I am planning to make slopes in my game(because mountain regions in story and that when I was messing around I found out a cool sling shot way for cool platforming puzzle ideas) But I want the sprite to rotate at the angle that is relative to the angle of the ground collision. I’m thinking to add in a code saying that “if the player is grounded and the angle of the collided object is not 0, then rotate to relative rotation” of some sort. I’m not sure how to execute in the “rotate to relative rotation” part though. :confused: The player body is a RigidBody2D, not a kinematic body.

:bust_in_silhouette: Reply From: bruteforce

If your “player” is a KinematicBody2D (and “grounded” = “collided with the ground”) then you can use the get _ collision _ normal() function, which returns the normal of the ground (below the character).
This normal (Vector2) is perpendicular to your slope, so you can calculate it’s angle (ie. with the Vector2’s angle_to(Vector2 to) function).

Ooo but I have A rigid body character because I was lazy to put in gravity >.< I forgot to say that, sorry.

Noob_Maker | 2017-01-02 12:58

:bust_in_silhouette: Reply From: eons

For rigid bodies, you can check the state in _integrate_forces, there you have access to get_contact_local_normal.
http://docs.godotengine.org/en/stable/classes/class_physics2ddirectbodystate.html#class-physics2ddirectbodystate-get-contact-local-normal

But may be better by just using a RayCast2D pointing down and only rotate the visual part, not the whole body if not needed (easier to manipulate).

Okay thanks!

Noob_Maker | 2017-01-02 15:24