SET the CFrame Orientation without changing Position

Hey there.

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.

Do you have a script already so yes, can you show me/us?

The script is pretty long and I don’t think that it will help in this case.

Here is a Video so you might understand my problem:

https://gyazo.com/4d67d40759e6ddc92b854a58c4d38f11

As you can clearly see, if I move my Camera as example behind the Character and press Twice “W”, then the Character will move sideways.

What I think the solution is:

Right before the Dashing Velocity begins, the players CFrame is set to the Cameras one. Like when you Shiftlock.

Assuming this code is in a LocalScript

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)

I only needed the SetRootPartCFrame() function, thanks!

One more question, can I somehow rotate the LookVector by lets say 180° if he is dashing backwards?

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.

1 Like

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