Автор - 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
Hide player controls
Hide resume playing