+1 vote

Hi! How can I calculate rotation of KinematicBody (3D) to make it following a slopes?

Right now my "box" is moving very well using moveandslide but I would like to rotate it in natural way to keep both sids on the floor like on attached picture (first is how it's working and second is what I would like to achieve:

enter image description here

in Engine by (227 points)

1 Answer

+1 vote
Best answer

So move and slide has a companion function that returns KinematicCollision2D objects:
do something like this

.. move and slide ..
if getslidecount() > 0:
var collision = getslidecollision(0)
var normal = collision.normal()
var angleDelta = normal.angle() - (rotation - PI)
rotation = angleDelta + rotation

If this is in a script attached to your Kinematic Body2D, then rotation is a buildin variable that imediately affects rotation.

Not so sure about the - PI, but your normal will be pointing away from the surface, and depending our your object, it might be PI * .5 or something. Experiment.
Note too, that when one of the angles crosses from -PI to +PI, you might need to flip things around. I'd use some print() statements to monitor the values you're getting for the angles in your setup.
Note in the above I'm just taking the first collision with getslidecollision(0); check the api for details.

by (68 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.