Make character move to the direction its moving

Hey devs :wave:,

I’ve made a post similar to this:
https://devforum.roblox.com/t/slope-alignment-clipping/2085632/11

But another problem I’ve run into is getting the character to face the direction
it’s moving in:

How do I do this without it interfering with the BodyGyro that makes the character rotate up the slopes?

local runService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local character = player.Character

local humanoid = character:WaitForChild("Humanoid")
local rootpart = humanoid.RootPart

local collider = Instance.new("Part")
collider.Name = "Collider"
collider.Transparency = 1
collider.Shape = Enum.PartType.Ball
collider.Size = Vector3.new(2, 2, 2)
collider.Massless = true
collider.CFrame = rootpart.CFrame * CFrame.new(0, -2, 0)
collider.Parent = character

local colliderWeld = Instance.new("WeldConstraint")
colliderWeld.Name = "ColliderWeld"
colliderWeld.Part0 = collider
colliderWeld.Part1 = rootpart
colliderWeld.Parent = rootpart

local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.MaxForce = Vector3.new(100000, 0, 100000)
bodyVelocity.Velocity = Vector3.new(0, 0, 0)
bodyVelocity.P = 100000
bodyVelocity.Parent = rootpart

local bodyGyro = Instance.new("BodyGyro")
bodyGyro.MaxTorque = Vector3.new(1000000, 0, 1000000)
bodyGyro.P = 35000
bodyGyro.CFrame = rootpart.CFrame
bodyGyro.Parent = rootpart

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {character}
raycastParams.IgnoreWater = true

local colliderPhysicalProperties = PhysicalProperties.new(
	0.7, -- Density
	1, -- Friction
	0, -- Elasticity,
	1, -- FrictionWeight
	1 -- ElasticityWeight
)

collider.CustomPhysicalProperties = colliderPhysicalProperties

--

humanoid.AutoRotate = false
humanoid.PlatformStand = true

--[[
    Moves the character, but how do I make it face the direction
    its moving without it interfering with the BodyGyro?
]]

function MoveCharacter()
	local velocity = humanoid.MoveDirection * humanoid.WalkSpeed	
	bodyVelocity.Velocity = velocity
end

-- Slope Tilting

function AlignToSurface()
	local direction = rootpart.CFrame.UpVector * -10
	local ray = game.Workspace:Raycast(rootpart.Position, direction, raycastParams)
	
	if ray then
		local hit, pos, normal = ray.Instance, ray.Position, ray.Normal
		
		local p1 = rootpart.CFrame.LookVector:Cross(normal)
		bodyGyro.CFrame = CFrame.fromMatrix(rootpart.Position, p1, normal)
	end
end

runService.Heartbeat:connect(AlignToSurface)
humanoid:GetPropertyChangedSignal("MoveDirection"):connect(MoveCharacter)

Any replies would help out a ton :grin:

Instead of utilizing rays, have you tried finding which way the player is facing from their orientation instead?

Character:GetPivot().Rotation

With this, you can offset a player’s position with respect to its orientation. I assume that it can fit the purpose of BodyGyros, but I’m currently not home to test this.