I’m currently working on making the mechanics for a skiing game and I thought the best way to do this is by attaching a sphere to the character and moving the sphere with force. I noticed that x_o made a game with the same concept and so I wanted to get some inspiration before making my own. While reading the script I found some math I didn’t understand and so I want to ask for some help. If anyone is ready to explain to me, I would really appreciate it! (hopefully this is the right category)
now here is the script the problem is inside:
RunService:BindToRenderStep("Control", Enum.RenderPriority.Character.Value+1, function(deltaTime)
-- grounded check
local floorRay = Ray.new(CHARACTER.Position, -floor.Unit * FLOOR_CHECK)
local hit, position, normal = Workspace:FindPartOnRayWithIgnoreList(floorRay, {CHARACTER})
local floorDistance = (position - CHARACTER.Position).Magnitude
if floorDistance <= HEIGHT then
grounded = true
else
grounded = false
end
if hit then
if grounded then
floor = normal
else
floor = floor:Lerp(normal, math.min(deltaTime * 5, 1))
end
else
floor = floor:Lerp(Vector3.new(0, 1, 0), math.min(deltaTime, 1))
end
-- floor cframe
local lookVector = CAMERA.CFrame.lookVector
lookVector = Vector3.new(lookVector.X, 0, lookVector.Z).Unit
local floorCFrame = CFrame.new(Vector3.new(), lookVector)
local localFloor = floorCFrame:vectorToObjectSpace(floor)
local x, y = math.atan2(-localFloor.X, localFloor.Y), math.atan2(localFloor.Z, localFloor.Y)
local cfA = CFrame.Angles(y, 0, 0) * CFrame.Angles(0, 0, x)
local cfB = CFrame.Angles(0, 0, x) * CFrame.Angles(y, 0, 0)
floorCFrame = floorCFrame * cfA:Lerp(cfB, 0.5)
-- input
if not UserInputService:GetFocusedTextBox() then
local input = Vector3.new()
if UserInputService:IsKeyDown(Enum.KeyCode.W) then
input = input + Vector3.new(0, 0, -1)
end
if UserInputService:IsKeyDown(Enum.KeyCode.S) then
input = input + Vector3.new(0, 0, 1)
end
if UserInputService:IsKeyDown(Enum.KeyCode.A) then
input = input + Vector3.new(-1, 0, 0)
end
if UserInputService:IsKeyDown(Enum.KeyCode.D) then
input = input + Vector3.new(1, 0, 0)
end
if input.Magnitude > 0 then
input = input.Unit
end
targetVelocity = floorCFrame:VectorToWorldSpace(input * SPEED)
end
-- apply force
if grounded then
FORCE.Force = (targetVelocity - CHARACTER.Velocity) * MASS * ACCELERATION
else
FORCE.Force = Vector3.new()
end
end)
This is a lot of code but the part I’m intrested in is where the force is applied towards the bottom of the script. What is the thought process here? Why is he using these values?