Godot 4 - How to Rotate Smoothly to Face a Direction
Автор - Thomas Yanuziello
ссылка -
От автора:
You have a character or other object that you want to face a specific direction, possibly the direction the player wants to move, either in 2D or 3D. But you want the rotation to be smooth over time, not just instantly snap to the direction.
Start by exporting a rotation speed, a float measured in radians per second.
If radians make no sense to you, you can define it as a multiple of PI or TAU. PI is a half circle, or 180 degrees, and TAU is a full circle, or 360 degrees. I’ll use TAU times 2, which is 2 full rotations per second. Also define a variable that represents the angle between the direction they are currently facing, and the direction we want them to face, THETA.
If the player is giving direction input, then we can calculate THETA to be atan2 of y over x. This provides the angle difference of a line segment connecting the origin of a 2D cartesian plane to these coordinates and the positive x axis. Subtracting the current rotation gives us the difference between that line segment and the current rotation. We also need to wrap this value to be between negative and positive PI. Remember that PI is a half rotation, so this is restricting the angle to be between a half rotation clockwise and a half rotation counterclockwise.
Then add to the character’s rotation, the rotation speed times DELTA, clamped to be no less than 0, and no more than the absolute value of THETA. This will prevent overshooting the mark, since the actual rotation can’t be more than theta. But we also need to apply the sign of THETA to the rotation speed, to rotate in the desired clockwise or counterclockwise direction.
In 3D, we are rotating the character rig, and its rotation is a vector3, but we only need to rotate about the y axis. We can use the same formula, substituting -z for y in the direction if using 3D direction, but also adding a quarter rotation, making backward, or away from the camera, the point of reference instead of right. Or we can simplify this a little bit by removing the quarter rotation, swapping x and z in the atan2 function, and removing the negative from z. This will still provide the angle difference between the directional input and the character rig’s current y rotation, so the rotation statement remains the same.
3D assets made by KayKit
13 views
484
129
4 weeks ago 00:04:30 1
Godot 4 - Tears of the Kingdom Style Clouds
4 weeks ago 00:10:18 1
Godot 4 - Wall of particles shader
4 weeks ago 00:06:12 1
Godot 4 - Bake 2D navmeshes with tileset collisions
4 weeks ago 00:06:04 1
Godot 4 - Make your own line renderer
4 weeks ago 00:27:08 1
Масс-маркет и средний сегмент: примеры маркетинга, VM и сервиса
4 weeks ago 00:00:40 1
Двигаем историю вместе!
4 weeks ago 00:00:40 1
Защита будущего России, достойная оплата и настоящее боевое братство
4 weeks ago 00:02:01 7
Godot 4 - How to make a BASIC FPS CLIMB - VAULT system
4 weeks ago 00:02:34 6
Godot 4 - How to do Collision Detection
4 weeks ago 00:00:44 1
Как улучшить камеру в играх / Плавное движение камеры
1 month ago 00:02:46 1
Jungle Demo V2 - Godot Engine 4
1 month ago 00:06:54 2
Improving Environment Graphics in Godot 4 - Tutorial
1 month ago 00:07:47 12
Godot 4 - How to make isometric pixelart worldmap
1 month ago 00:03:28 14
Godot 4 - How to Create Improved Decal Bullet holes
1 month ago 00:21:20 15
Замена датчика колодок Discovery 3
1 month ago 00:11:13 2
Автоподбор Range Rover Autobiography 2015г
1 month ago 00:12:27 12
Диагности Range Rover Autobiography 2013г, 4.4 Diesel
1 month ago 00:05:31 1
UTRAI Jstar 5 --- 4 в 1 Автомобильное пусковое устройство + автоматический насос.
1 month ago 00:17:57 2
Предпродажная диагностика ИДЕАЛЬНОГО Range Rover Autobiography 2013г.
1 month ago 00:18:03 9
Выездная диагностика Range Rover Sport 2013
1 month ago 00:10:24 7
Диагностика Range Rover Voge (MAMA) перед продажей!
1 month ago 00:30:34 1
Два Range Rover с разницей в миллион рублей.
1 month ago 00:37:18 3
Очередной Автосалон пробит дно! Осмотр Range Rover SALLMAMA 2011 год, за 1,7 миллиона
1 month ago 00:12:05 8
Идеальная МАМА, по мнению перекупа. Range Rover Autobiography. Автоподбор Land Rover.