How to snap character rotation?

So I’m making a dungeon crawler with custom characters, and I’ve ran into an issue.

What do I want to achieve? I’m trying to make a dodge/roll system where when I press “Q”, it dodges based on which direction you’re going. Sort of like TSB.
Example:

What’s the issue? The problem is when the player dodges backwards the player faces forwards then dodges. But instead of a snap rotation, it is a slow turn.

What solutions have I tried so far? I’ve tried different ways to set rotation. I’ve also tried disabling the players controls and disabled autoRotate.

Here is my code:

-- local Player
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local PrimaryPart = Character.PrimaryPart

-- UIS
local UIS = game:GetService("UserInputService")

-- Tween Service
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local distance = 10
local Direction = nil -- Direction Variable

UIS.InputBegan:Connect(function(Input, GameProcessedEvent)
	if GameProcessedEvent then return end

	-- Direction -- 
	task.wait()
	if Humanoid.MoveDirection.Magnitude > 0 then
		if Input.KeyCode == Enum.KeyCode.W then
			Direction = "W"
		elseif Input.KeyCode == Enum.KeyCode.S then
			Direction = "S"
		elseif Input.KeyCode == Enum.KeyCode.A then
			Direction = "A"
		elseif Input.KeyCode == Enum.KeyCode.D then
			Direction = "D"
		end
	else
		Direction = nil
	end
	print(Humanoid.MoveDirection.Magnitude)
	
	-- Dodge --
	if Input.KeyCode == Enum.KeyCode.Q then
		Dodge(Direction)
		print(Direction)
	end
end)

function Dodge(direction)
	local Controls = require(Player.PlayerScripts.PlayerModule):GetControls()
	local tween
	-- Camera rotation.
	if direction == "W" then
		tween = TweenService:Create(PrimaryPart, tweenInfo, {CFrame = PrimaryPart.CFrame * CFrame.new(0, 0, -distance) })
		tween:Play()
	elseif direction == "S" then
		local characterPos = Character.HumanoidRootPart.CFrame
		local moveDir = Humanoid.MoveDirection*2
		moveDir = Vector3.new(moveDir.X, 0, moveDir.Z)
		characterPos = CFrame.new(characterPos.p, characterPos.p + -moveDir)
		tween = TweenService:Create(PrimaryPart, tweenInfo, {CFrame = characterPos * CFrame.new(0, 0, distance) })
		tween:Play()
	else
		tween = TweenService:Create(PrimaryPart, tweenInfo, {CFrame = PrimaryPart.CFrame * CFrame.new(0, 0, -distance) })
		tween:Play()
	end
end

Any help would be great. Thanks!

-Mattc04

1 Like

Nevermind I figured it out!
I basically used PivotTo() to set players position to the opposite direction and updated the tween.
I also made it go backwards from the camera when the player has shift lock on!

Here is my code if anyone else has this issue!

  • Mattc04
-- local Player
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local PrimaryPart = Character.PrimaryPart

-- UIS
local UIS = game:GetService("UserInputService")

-- Tween Service
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local distance = 10
local Direction = nil -- Direction Variable
local ShiftLock = nil

UIS.InputBegan:Connect(function(Input, GameProcessedEvent)
	if GameProcessedEvent then return end

	-- Direction -- 
	task.wait()
	if Humanoid.MoveDirection.Magnitude > 0 then
		if Input.KeyCode == Enum.KeyCode.W then
			Direction = "W"
		elseif Input.KeyCode == Enum.KeyCode.S then
			Direction = "S"
		elseif Input.KeyCode == Enum.KeyCode.A then
			Direction = "A"
		elseif Input.KeyCode == Enum.KeyCode.D then
			Direction = "D"
		end
	else
		Direction = nil
	end
	print(Humanoid.MoveDirection.Magnitude)
	
	-- Dodge --
	if Input.KeyCode == Enum.KeyCode.Q then
		Dodge(Direction)
		print(Direction)
	end
end)

function Dodge(direction)
	local Controls = require(Player.PlayerScripts.PlayerModule):GetControls()
	local tween
	-- Camera rotation.
	if direction == "W" then
		tween = TweenService:Create(PrimaryPart, tweenInfo, {CFrame = PrimaryPart.CFrame * CFrame.new(0, 0, -distance) })
		tween:Play()
	elseif direction == "S" then
		local characterPos = Character.HumanoidRootPart.CFrame
		local modelCFrame = Character:GetPivot()
		print(ShiftLock)
		if ShiftLock ~= true then
			Character:PivotTo(modelCFrame * CFrame.Angles(0, math.rad(180), 0))
			tween = TweenService:Create(PrimaryPart, tweenInfo, {CFrame = characterPos * CFrame.new(0, 0, -distance) * CFrame.Angles(0, math.rad(180), 0)})
		else
			tween = TweenService:Create(PrimaryPart, tweenInfo, {CFrame = characterPos * CFrame.new(0, 0, distance)})
		end
		tween:Play()
	else
		tween = TweenService:Create(PrimaryPart, tweenInfo, {CFrame = PrimaryPart.CFrame * CFrame.new(0, 0, -distance) })
		tween:Play()
	end
end

while true do task.wait()
	if UIS.MouseBehavior == Enum.MouseBehavior.LockCenter then
		ShiftLock = true
	else
		ShiftLock = false
	end
	task.wait()
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.