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
44 views
484
129
1 month ago 00:05:21 3
Godot 4 - How to stop enemies falling off platforms using a RayCast
1 month ago 00:09:38 12
Godot 4 - 3D Dice Rolling Animation
1 month ago 00:36:07 22
Godot 4 - A Complete Guide to Y-SORTING
1 month ago 00:14:50 19
Godot 4 - Build Sudoku Game
1 month ago 00:05:22 16
Godot 4 - Random Items with Rarity Logic
1 month ago 00:07:06 15
Godot 4 - How to make things float
1 month ago 00:05:48 18
Godot 4 - Button Push
1 month ago 00:06:27 23
Godot 4 - 3D overlay on 3D scene
1 month ago 00:02:58 32
Godot 4 - Mesh Masks To See-through Other Objects
1 month ago 00:41:21 127
Godot 4 - Your First 3D Platformer Tutorial
1 month ago 00:04:09 25
Godot 4 - Analog Clock
1 month ago 00:18:05 27
Godot 4 - Making Minesweeper
1 month ago 00:36:39 552
Godot Engine 4 - Создание базового Java / Kotlin плагина для игры в Android Studio
1 month ago 00:09:39 33
Godot 4 - Rigid Body Character Controller
1 month ago 00:15:55 31
Godot 4 - Visibility Occlusion
1 month ago 00:02:07 18
Godot 4 - Create a 3D Minimap
1 month ago 00:14:37 21
Godot 4 - CARD GAME Tutorial (part 7) - Create an OPPONENT
1 month ago 00:02:10 583
Automate Level Design with Tiled Automapping & Godot 4.3 C#