I am currently making a Dash script and short before the player is dashing, the HumanoidRootParts CFrame Orientation should be set to the Camera so he is always dashing forward where the Camera looks to.
Does anybody know how to do that? I’ve tried so many posts but no solution yet.
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera
-- Function to set the CFrame of HumanoidRootPart based on the camera orientation
local function setRootPartCFrame()
local cameraCFrame = camera.CFrame
rootPart.CFrame = CFrame.new(rootPart.Position, rootPart.Position + cameraCFrame.LookVector)
end
-- Connect the function to the RenderStepped event to update continuously
game:GetService("RunService").RenderStepped:Connect(setRootPartCFrame)
-- Example: Trigger this when the player presses "W" twice to dash
-- Replace this with your actual dash logic
local function onDash()
print("Dashing!")
-- Your dash logic here
end
-- Example: Connect the onDash function to the user input
-- Replace this with your actual input handling logic
-- For instance, you might use a UserInputService to detect key presses
game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessed)
if not gameProcessed and input.KeyCode == Enum.KeyCode.W then
onDash()
end
end)
You can modify the LookVector based on the movement direction. If the character is dashing backward, you can rotate the LookVector by 180 degrees.
local function setRootPartCFrame()
local cameraCFrame = camera.CFrame
local movementDirection = cameraCFrame.LookVector
-- Check if the player is dashing backward
if isDashingBackward then
-- Rotate the movement direction by 180 degrees
movementDirection = CFrame.Angles(0, math.pi, 0) * movementDirection
end
rootPart.CFrame = CFrame.new(rootPart.Position, rootPart.Position + movementDirection)
end
In this example, isDashingBackward is a variable that you would set to true when the character is dashing backward. If the character is dashing backward, it rotates the movementDirection by 180 degrees using CFrame.Angles(0, math.pi, 0).
Make sure to set isDashingBackward to the correct value based on your actual dash logic. This is just a placeholder for the condition that determines whether the character is dashing backward.