Rolling Mechanic

I’ve been trying to make a roll mechanic but I can just never get it to work, I’ve tried various ways and the closest I’ve got is using LookVector and BodyForce.

If anyone has information about how to create a rolling mechanic please link me to the post or explain.
Thanks.

3 Likes

Some considerations before scripting it is the type of roll:

Roll in the direction of the camera, or the direction of movement.
Turn the roll as the camera/movement turns or have the roll direction fixed.

Generally, to make a roll mechanic, you create a roll animation which rolls in place, and then you use either CFrames or Attachments and AlignPosition to make the character roll.

Pseudocode:

When the user presses the roll input
Check whatever conditions you have to make sure it's valid, could be stamina etc
Play the rolling animation on the Humanoid
Use either the Camera or Humanoids LookVector and store it
Apply the stored CFrame to the Humanoid's CFrame in a for loop or use dT and RunService

You can get fancier and do stuff like projecting whatever movement direction vector to be in the plane of the surface the humanoid is on, getting the normal with a raycast, but the above is the simplest method and still looks as good.

1 Like

This confuses me a lot, you mind leaving a snippet of code I can look into? Or example?

I suggest using a combination of HumanoidRootPart’s lookVector, BodyVelocities, and like the other person said, animations that roll “in place”. Handle the movement of the character with the BodyVelocity.

If I make a Velocity and set it Z to 100000 for example its just going to move the player to the backwards because its the worlds xyz and not the way the players facing, and I don’t know how to set the body velocity that its the lookVector…

Sorry I just never used Velocitys.

Okay, so for example ,a roll that is in the direction the camera was facing when the roll key was pressed, moving the character in the heartbeat runservice event:

You need to track when a roll started, the direction to roll in, the roll length and the time since the last heartbeat.

local Humanoid = script.Parent:WaitForChild("Humanoid") --CharacterScripts copied to Char on load, wait for Humanoid
local RootPart = script.Parent:WaitForChild("HumanoidRootPart")
local RollTrack = Humanoid:LoadAnimation(game.ReplicatedStorage:WaitForChild("RollAnimation"))
repeat wait() until RollTrack.Length ~= 0
--Wait for the animation to load properly
local RollStartTime = 0
local RollDirection = Vector3.new()
local RollMult = 15/RollTrack.Length --15 studs / AnimLength

use UserInputService to check for when the user presses the roll key:

InputService.InputBegan:Connect(function(input,processed)
	if input and not processed then
		--If roblox has not handled the input (GUI textbox, esc key, alt tab etc, gui buttons clicked)
		if input.UserInputType == Enum.UserInputType.Keyboard then
			--if it is keyboard input
			if input.KeyCode == RollKey and tick()-RollStartTime > .75 then
				--Do a roll
				RollTrack:Play()
				RollStartTime = tick()
				local dirVec = workspace.CurrentCamera.CFrame.LookVector
				RollDirection =  (dirVec*Vector3.new(1,0,1)).unit
				--normalise in horizontal plane
			end
		end
	end
end)

In the heartbeat calculate the time since the last for a delta, dT, and apply the roll CFrame to the HumanoidRootPart to move the character.

local LastBeat = tick()
RunService.Heartbeat:Connect(function()
	local dT = tick()-LastBeat
	LastBeat = tick()
	
	if tick()-RollStartTime < .75 then
		RootPart.CFrame = CFrame.new(RollDirection*dT*RollMult) * RootPart.CFrame
	end
end) 

The RollDirection vector is in world space, so just apply it before the RootPart CFrame, to offset the world grid rather than transform the rootpart cframe. RollMult will be the number of studs to move by divided by the animation length.

28 Likes