Turn script problem!

  1. What do you want to achieve?
    Actually the player turn but very curtly !
    So I want to the player rotate but not teleport his rotation !
  2. What is the issue?
    The player rotate curtly !
  3. What solutions have you tried so far?
    I have this code Airzy_Az helped me a lot :
while wait() do
	if HumanoidRootPart.CFrame:VectorToObjectSpace(Camera.CFrame.LookVector).Z > 0 and Aim == true and Idle == true then
		local cameraDirection = Camera.CFrame.Rotation
		local rotX,rotY,rotZ = cameraDirection:ToEulerAnglesYXZ()
		local rootPartPos = HumanoidRootPart.CFrame.Position
		local RootRotX,RootRotY,RootRotZ = HumanoidRootPart.CFrame:ToEulerAnglesYXZ()
		HumanoidRootPart.CFrame = CFrame.new(rootPartPos)*CFrame.Angles(RootRotX,rotY,RootRotZ)
	end
end
1 Like

You’re going to want to use TweenService for this, and the concept is called interpolation - you want to interpolate from the starting rotation to the ending rotation.

2 Likes
local runService = game:GetService("RunService")

 -- how fast we want the character to rotate value can be from 0 to 1
local turnSpeed = 0.1

runService.Heartbeat:Connect(function(deltaTime)
    if HumanoidRootPart.CFrame:VectorToObjectSpace(Camera.CFrame.LookVector).Z <= 0 then return end
    if Aim == false then return end
    if Idle == false then return end

    -- get the direction the camera is facing
    local cameraLookVector = Camera.CFrame.LookVector

    -- remove the Y axis from the cameras look vector
    local targetLookVector = Vector3.new(cameraLookVector.X, 0, cameraLookVector.Z).Unit

    -- crate a new cframe using the targetLookVector
    local targetCframe = CFrame.LookAt(HumanoidRootPart.Position, HumanoidRootPart.Position + targetLookVector)

    -- lerp the the players cframe to the targetCframe
    HumanoidRootPart.CFrame = HumanoidRootPart.CFrame:Lerp(targetCframe, turnSpeed)
end)
2 Likes
local character = script.Parent
local plr = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
local runService = game:GetService("RunService")

-- how fast we want the character to rotate value can be from 0 to 1
local turnSpeed = 0.1

runService.Heartbeat:Connect(function(deltaTime)
	local camlook = Camera.CFrame.LookVector
	local targetCF = CFrame.lookAt(HumanoidRootPart.Position,Vector3.new(camlook.X,0,camlook.Z) * 100)
	HumanoidRootPart.CFrame = HumanoidRootPart.CFrame:Lerp(targetCF,turnSpeed)
end)

or if you want to use your code up there you can just add lerp

while wait() do
	if HumanoidRootPart.CFrame:VectorToObjectSpace(Camera.CFrame.LookVector).Z > 0 and Aim == true and Idle == true then
		local cameraDirection = Camera.CFrame.Rotation
		local rotX,rotY,rotZ = cameraDirection:ToEulerAnglesYXZ()
		local rootPartPos = HumanoidRootPart.CFrame.Position
		local RootRotX,RootRotY,RootRotZ = HumanoidRootPart.CFrame:ToEulerAnglesYXZ()
		HumanoidRootPart.CFrame = HumanoidRootPart.CFrame:Lerp(CFrame.new(rootPartPos)*CFrame.Angles(RootRotX,rotY,RootRotZ),0.1)
	end
end
1 Like

I’ve do this but it didn’t work … it’s the first time I use tweenService :neutral_face:

local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(
	0.5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	-1,
	true,
	0
)

while wait() do
	if HumanoidRootPart.CFrame:VectorToObjectSpace(Camera.CFrame.LookVector).Z > 0 and Aim == true and Idle == true then
		local cameraDirection = Camera.CFrame.Rotation
		local rotX,rotY,rotZ = cameraDirection:ToEulerAnglesYXZ()
		local rootPartPos = HumanoidRootPart.CFrame.Position
		local RootRotX,RootRotY,RootRotZ = HumanoidRootPart.CFrame:ToEulerAnglesYXZ()
		TweenService:Create(HumanoidRootPart, tweenInfo, CFrame.new(rootPartPos)*CFrame.Angles(RootRotX,rotY,RootRotZ))
	end
end

That print that in the ouptut :
Unable to cast to Dictionary
But thanks for the help :grin: !

1 Like

Thank you but that not what I want exactly, For exemple if the camera reach a certain axis the player rotate 90° and I want to do the same but the player turn not teleport ! :wink:

I’ve the same problem I have with 5uphi !
But thank you too ! :grinning:

1 Like

Any help would be appreciated ! :wink:

local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(
	0.5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	-1,
	true,
	0
)

while wait() do
	if HumanoidRootPart.CFrame:VectorToObjectSpace(Camera.CFrame.LookVector).Z > 0 and Aim == true and Idle == true then
		local cameraDirection = Camera.CFrame.Rotation
		local rotX,rotY,rotZ = cameraDirection:ToEulerAnglesYXZ()
		local rootPartPos = HumanoidRootPart.CFrame.Position
		local RootRotX,RootRotY,RootRotZ = HumanoidRootPart.CFrame:ToEulerAnglesYXZ()
		TweenService:Create(HumanoidRootPart, tweenInfo, {CFrame = CFrame.new(rootPartPos)*CFrame.Angles(RootRotX,rotY,RootRotZ)})
	end
end

when using tweenservice the 3rd parameters is a table, and inside the table will be propertyName = propertyValue.

2 Likes

Thank you, I tested it and it doesn’t work … :neutral_face:
Maybe I’ve made a mistakes ? :thinking:

oh you need to play it lol.

local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(
	0.5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	-1,
	true,
	0
)

while wait() do
	if HumanoidRootPart.CFrame:VectorToObjectSpace(Camera.CFrame.LookVector).Z > 0 and Aim == true and Idle == true then
		local cameraDirection = Camera.CFrame.Rotation
		local rotX,rotY,rotZ = cameraDirection:ToEulerAnglesYXZ()
		local rootPartPos = HumanoidRootPart.CFrame.Position
		local RootRotX,RootRotY,RootRotZ = HumanoidRootPart.CFrame:ToEulerAnglesYXZ()
		TweenService:Create(HumanoidRootPart, tweenInfo, {CFrame = CFrame.new(rootPartPos)*CFrame.Angles(RootRotX,rotY,RootRotZ)}):Play()
	end
end

after you created a tween using Tweenservice:Create() it will return a tween object. now you can play/stop the tween object
Example :

local tweenObject = TweenService:Create(object,tweeninfo,{properties})
tweenObject:Play() -- if you dont call this function the tween wouldnt play.
2 Likes

Thank you so much ! :grin:
But I have a little issue, the animation wouldn’t stop … :neutral_face:

1 Like

put reverse to false so it doesnt bounce back and forth.
also make a variable to stop the old tween and update the new tween.
(P.S this current code i think will cost a lot of performance)

local t -- a variable to store tween so we can play and cancel tween.
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(
	0.5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	-1,
	false,
	0
)

while wait() do
	if HumanoidRootPart.CFrame:VectorToObjectSpace(Camera.CFrame.LookVector).Z > 0 and Aim == true and Idle == true then
		local cameraDirection = Camera.CFrame.Rotation
		local rotX,rotY,rotZ = cameraDirection:ToEulerAnglesYXZ()
		local rootPartPos = HumanoidRootPart.CFrame.Position
		local RootRotX,RootRotY,RootRotZ = HumanoidRootPart.CFrame:ToEulerAnglesYXZ()
        if t then t:Cancel() t = nil end -- add this to stop the old tween
		t = TweenService:Create(HumanoidRootPart, tweenInfo, {CFrame = CFrame.new(rootPartPos)*CFrame.Angles(RootRotX,rotY,RootRotZ)}) -- creating new tween
        t:Play() -- play the new tween
	end
end

but i suggest rather than looping the tween, you might just want to tween once then lock.
or another alternative is to just use bodygyro, put the bodygyro inside humanoidrootpart and change the bodygyro CFrame to the one you specified.

1 Like

Thank you very much I fixed it I made a little mistakes here :sweat_smile::grin: :

local Tween -- a variable to store tween so we can play and cancel tween.
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(
	0.25,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0, --HERE--
	false,
	0
)

while wait() do
	if HumanoidRootPart.CFrame:VectorToObjectSpace(Camera.CFrame.LookVector).Z > 0 and Aim == true and Idle == true then
		local cameraDirection = Camera.CFrame.Rotation
		local rotX,rotY,rotZ = cameraDirection:ToEulerAnglesYXZ()
		local rootPartPos = HumanoidRootPart.CFrame.Position
		local RootRotX,RootRotY,RootRotZ = HumanoidRootPart.CFrame:ToEulerAnglesYXZ()
		Tween = TweenService:Create(HumanoidRootPart, tweenInfo, {CFrame = CFrame.new(rootPartPos)*CFrame.Angles(RootRotX,rotY,RootRotZ)}) -- creating new tween
		Tween:Play()
	end
end

Have a nice day ! :wink: