I’ve been making a roll/dive mechanic for my platformer game, and I’ve come across an issue where if they roll/dive into a wall, it ragdolls the player. One of the developers suggested adding a bonking mechanic to stop the player from ragdolling, but I don’t know how to even begin making this. So far, the bonk system I’ve made causes the player to ragdoll pretty far. Please give examples when explaining.
Current Script:
Character.HumanoidRootPart.Touched:Connect(function()
if Diving or Rolling then
Animations.Dive:Stop()
Animations.Roll:Stop()
if Character.HumanoidRootPart:FindFirstChild("BodyVelocity") then
Character.HumanoidRootPart.BodyVelocity.Velocity = -(Character.HumanoidRootPart.BodyVelocity.Velocity * .75)
task.wait(1.5)
Character.HumanoidRootPart.BodyVelocity:Destroy()
end
end
You could maybe shoot a raycast to constantly check if a player is colliding with a wall. If they do collide with the wall, stop them from diving and rolling and send them back and play an animation.
local rayOrigin = Vector3.zero
local rayDirection = Vector3.new(0, -100, 0)
local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
if raycastResult then
print("Instance:", raycastResult.Instance)
print("Position:", raycastResult.Position)
print("Distance:", raycastResult.Distance)
print("Material:", raycastResult.Material)
print("Normal:", raycastResult.Normal)
else
warn("No raycast result!")
end
Although in your case, you prolly should fire the raycast in a while loop or run service loop and check if there’s a raycast result. If it is then do whatever you need to do to confirm that it’s a wall, and then break the loop and stop them from diving or rolling.
I have a question; how would I make the player bounce back when “hitting” the wall? Would I just change the BodyVelocity’s velocity to its negative value? Because currently, it ragdolls the player.
local function Bonk()
if Diving or Rolling then
Animations.Dive:Stop()
Animations.Roll:Stop()
Animations.Jump:Stop()
Animations.Run:Stop()
Animations.Idle:Stop()
Animations.Bonk:Play()
if Character.HumanoidRootPart:FindFirstChild("BodyVelocity") then
Character.HumanoidRootPart.BodyVelocity.Velocity = Vector3.new(0, 0, 0)
--Character.HumanoidRootPart.BodyVelocity.Velocity = -(Character.HumanoidRootPart.BodyVelocity.Velocity * .5)
task.wait(0.75)
Character.HumanoidRootPart.BodyVelocity:Destroy()
task.delay(0.5, function()
Bonked = false
end)
end
end
end
local function CheckBonk()
local rayOrigin = Character.HumanoidRootPart.Position
local rayDirection = Vector3.new(0, -100, 0)
local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
if raycastResult then
if raycastResult.Normal == Enum.NormalId.Back or raycastResult.Normal == Enum.NormalId.Left or raycastResult.Normal == Enum.NormalId.Right or raycastResult.Normal == Enum.NormalId.Front then
if raycastResult.Distance < 2 then
if not Bonked then
Bonked = true
Bonk()
end
end
end
--[[print("Instance:", raycastResult.Instance)
print("Position:", raycastResult.Position)
print("Distance:", raycastResult.Distance)
print("Material:", raycastResult.Material)
print("Normal:", raycastResult.Normal)]]
end
end
For a bit more context on what I’m trying to create, is the Bonking mechanic in Robot 64 or Super Mario 64 when you dive into a wall, it makes the player bounce back a bit.
It works a bit, but the player gets stuck in the wall.