How to implement natural rotation of a body, more specifically a plane. in 2D ?
I am using a on screen joystick controller to move the body. and whenever I move the joystick to any direction the body instantly turns to that direction.
How to rotate the body smoothly in a natural way. ?
(How I want it to rotate) --

(How I don't want it to rotate) --

The part of the code meant for rotation --
# Angle between the direction in which the body is facing the
# joystick_vector (i.e the direction in which the joystick is pushed/stretched)
angle = direction.angle_to(joystick.joystick_vector);
print(angle);
if (angle > 0):
rotation += deg2rad(unit_rotation * delta);
elif(angle < 0):
rotation -= deg2rad(unit_rotation * delta);
The Full code for the Player --
extends KinematicBody2D
export (NodePath) var joystick_path;
export (int) var joystick_speed = 500;
export (int) var normal_speed = 300;
export (float) var unit_rotation = 60
#var speed;
var joystick;
# The direction in which the player is/was moving.
var direction = Vector2()
# The extent to which the player have to rotate
var angle
const JOYSTICK_DEADZONE = 0.4;
func _ready():
$AnimatedSprite.animation = "Normal"
direction = Vector2(0,1)
position = get_viewport().size/2;
joystick = get_node(joystick_path);
func _physics_process(delta):
# Move based on the joystick, only if the joystick is farther than the dead zone.
if (joystick.joystick_vector.length() > JOYSTICK_DEADZONE):
move_and_collide(-joystick.joystick_vector * joystick_speed * delta);
# Angle between the direction in which the body is facing the
# joystick_vector (i.e the direction in which the joystick is pushed/stretched)
angle = direction.angle_to(joystick.joystick_vector);
print(angle);
if (angle > 0):
rotation += deg2rad(unit_rotation * delta);
elif(angle < 0):
rotation -= deg2rad(unit_rotation * delta);
# When the joystick is not in work.
else :
move_and_collide(-direction * normal_speed * delta);