I’ve been trying to replicate a system like the linked post above, but I’ve been struggling for a week or two (evident from my previous posts). I’ve tried reaching out to @Czlopek for assistance but they haven’t posted for over 2 years so I doubt they’d respond.
Additionally, the tutorial that they used uses deprecated objects such as BodyPosition and BodyGyro which only confuses me even more.
Currently I’m trying to rewrite my code, but I’d like some people to help guide me to the right direction and provide tips.
BodyPosition and BodyGyro are deprecated but roblox docs page about them show alternatives, AlignPosition and AlignOrientation respectfully, from what i remember they work very similarly.
My implementation made the player walk speed = 0 then i had a ray cast to check if there were any obstacles in the way, coming from the player, then i would create BodyGyro and BodyPosition pointed towards that ray cast, play an animation, and after reaching the destination/animation finishing, i would break all of the bodymovers and return player speed to default.
I don’t have the code itself and i don’t have time right now to recreate it, but i remember it being quite hacky, good for singleplayer experiences but not so much in multiplayer. I’m busy with another nonroblox project so i hope this helped.
Here is a break down of how you might do the code:
Put a Script with local RunContext (or a LocalScript) in StarterCharacterScripts
Get the Humanoid with script.Parent:WaitForChild("Humanoid")
Make a connection to your input (e.g. key press, controller button, etc)
When the input event happens:
Get the Humanoid’s walking direction with humanoid.MoveDirection
Round the move direction to the nearest 45 degree angle. Here is some code mostly from AI:
function roundToNearest45(vx, vy)
-- Calculate the angle of the vector in radians
local angle = math.atan2(vy, vx)
-- Convert the angle to degrees
local degrees = math.deg(angle)
-- Round the angle to the nearest multiple of 45 degrees
local roundedDegrees = math.floor((degrees + 22.5) / 45) * 45
-- Convert the rounded angle back to radians
local roundedRadians = math.rad(roundedDegrees)
-- Calculate the new vector components
local newVx = math.cos(roundedRadians)
local newVy = math.sin(roundedRadians)
return newVx, newVy
end
-- Example usage
moveDirection = humanoid.MoveDirection
rx, ry = roundToNearest45(moveDirection.X, moveDirection.Y)
dashDirection = Vector3.new(rx, 0, ry)
Add a linear velocity in the direction above (rx, 0, ry) to the humanoid and delete it after waiting a bit.
For the linear velocity, you can remove any upwards force by setting the limits to per axis and setting the y axis force to zero. If you search up “linear velocity dash” on the forums you should find some helpful posts for setting that up.