I have a simple part of this LocalScript that rotates the players’ character depending on what WASD buttons they press, but for some reason, it will rotate the player the longer route instead of going the shorter way.
Section from LocalScript:
local facingDirection = "back"
function rotateCharacter(direction: string)
if direction == "front" then
tweenService:Create(player, rotateTweenInfo, {Orientation = Vector3.new(0, 0, 0)}):Play()
elseif direction == "back" then
if facingDirection == "right" then
tweenService:Create(player, rotateTweenInfo, {Orientation = Vector3.new(0, -180, 0)}):Play()
else
tweenService:Create(player, rotateTweenInfo, {Orientation = Vector3.new(0, 180, 0)}):Play()
end
elseif direction == "left" then
tweenService:Create(player, rotateTweenInfo, {Orientation = Vector3.new(0, 90, 0)}):Play()
elseif direction == "right" then
tweenService:Create(player, rotateTweenInfo, {Orientation = Vector3.new(0, -90, 0)}):Play()
end
facingDirection = direction
print(facingDirection)
end
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local rotateTweenInfo = TweenInfo.new(
0.2,
Enum.EasingStyle.Quad,
Enum.EasingDirection.Out
)
local part = workspace:WaitForChild("Part")
local facingDirection = nil
local directionCFrames = {
front = CFrame.Angles(0, math.rad(0), 0),
back = CFrame.Angles(0, math.rad(180), 0),
left = CFrame.Angles(0, math.rad(90), 0),
right = CFrame.Angles(0, math.rad(-90), 0),
}
local function rotatePart(direction)
local rotation = directionCFrames[direction]
if not rotation then return end
local targetCFrame = CFrame.new(part.Position) * rotation
if facingDirection ~= direction then
TweenService:Create(part, rotateTweenInfo, {CFrame = targetCFrame}):Play()
facingDirection = direction
print("Part Direction = ", direction)
end
end
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.W then
rotatePart("front")
elseif input.KeyCode == Enum.KeyCode.S then
rotatePart("back")
elseif input.KeyCode == Enum.KeyCode.A then
rotatePart("left")
elseif input.KeyCode == Enum.KeyCode.D then
rotatePart("right")
end
end
end)
to achieve the result you wanted
Interpretation:
When you use Orientation = Vector3.new(...), Roblox rotates the character based purely on the difference in angles. That means if you’re facing right (-90°) and want to face left (90°), it will rotate the long way around (180°), instead of just turning 90° the other direction.
The solution is to use CFrame instead. When you tween the CFrame, Roblox automatically chooses the shortest rotation path, which makes the rotation look more natural and smooth.