My game uses a custom movement system using BodyVelocity and BodyGyro. I’m trying to give characters a better collision system using a part with collision on so that they can’t walk through terrain or objects. However giving a player a part with collision causes them to tilt or spin sporadically when they collide with things. I want players to not spin or tilt when they collide with something, just to be unable to move through it. Every solution I look at just mentions using BodyGyro, but I’m confused because they already have BodyGyro.
This is the code I use to apply force and turn the character the way I want them to face, which is used every frame, using BodyGyro and BodyVelocity parented to the HumanoidRootPart:
function Movement.rotate(condition)
-- Rotation true
if condition == true then
-- TargetAngle represents the direction the character wants to be facing
CharGyro.MaxTorque = Vector3.new(0, rotationSpeed, 0)
CharGyro.CFrame = workspace.CurrentCamera.CFrame:ToWorldSpace(Movement.TargetAngle)
-- This causes the character to slowly turn to face that direction
else
-- Cease rotation
CharGyro.CFrame = HumanoidRootPart.CFrame
end
end
-- Move the character forward in the direction they are facing
function Movement.applyForwardVelocity(condition)
if condition == true then
-- Get values to be applied
local rotation = HumanoidRootPart.Orientation.Y
local speed = currentSpeed
local Xdirection = math.sin(0.0174532925*rotation)*(speed * -1)
local Zdirection = math.cos(0.0174532925*rotation)*(speed * -1)
local newVelocity = Vector3.new(Xdirection, CharVelocity.Velocity.Y, Zdirection)
HumanoidRootPart.BodyVelocity.Velocity = newVelocity
else
-- Cease velocity
HumanoidRootPart.BodyVelocity.Velocity = Vector3.new(0, CharVelocity.Velocity.Y, 0)
end
end
However, when I enable collision on the character’s HumanoidRootPart this is what happens
robloxapp-20220216-1826420.wmv (1.8 MB)
How do I stop the character from spinning uncontrollably when they collide with something?