Hello. I am currently having an issue where I’m trying to rotate a model using CFrames while the model is still moving. So far, I’ve been able to rotate the model, but I’m stumped on the model freezing once it’s rotated. Here’s what I mean:
You can see in the video that when I swing the axe without moving; everything is fine. But if I swing the axe while moving, the character freezes for a moment, then unfreezes.
Here’s the code I used to achieve what I’ve got so far (the model is all welded together):
If anyone has any ideas on how to fix this, please let me know! I would also like for the character to continue facing the mouse even while it’s swinging like in the video.
I’m not sure if tweenservice can be used in this situation, because the tween tries to keep the position the same while it’s tweening the rotation, and it ignores the changes in mouse Position. You could do the rotation interpolation using a runservice event.
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local ROTATION_TIME = .2
local rotationCf = CFrame.Angles(0, math.rad(130), 0)
local humanoidRootPart = -- the humanoidRootPart
local camera = workspace.CurrentCamera
-- linear is a value between 0 and 1
local function getQuadOutEasingVal(linear)
return 1 - (1 - linear) ^ 2
end
local function getMouseFacingCf(hrpPos)
local mousePos = UserInputService:GetMouseLocation()
local unitRay = Camera:ViewportPointToRay(mousePos.X, mousePos.Y)
local rayResult = workspace:RayCast(unitRay.Origin, unitRay.Direction * 5000)
local mouseWorldPos = rayResult.Position
return CFrame.lookAt(hrpPos, Vector3.new(mouseWorldPos.X, hrpPos.Y, mouseWorldPos.Z))
end
local function rotateHrp(rotateTime, rotationCf, easingFunct)
local startTime = os.clock()
repeat
RunService.RenderStepped:Wait()
local timeDiff = os.clock() - startTime
local t = getQuadOutEasingVal(timeDiff / rotationTime)
local currentRotationCf = CFrame.new():Lerp(rotationCf, t)
humanoidRootPart.CFrame = getMouseFacingCf(humanoidRootPart.Position) * currentRotationCf
until timeDiff >= rotateTime
humanoidRootPart.CFrame = getMouseFacingCf(humanoidRootPart.Position) * rotationCf
end
local function swingAxe()
rotateHrp(ROTATION_TIME, rotationCf, getQuadOutEasingVal)
end
Edit: I realized that you could also use TweenService:GetValue() to get t for interpolating the rotation cframe.