I am creating a game whereby players control a marble to complete courses, and eventually earn coins. The marble giver I am using is controlled using WASD (E to slow down), and uses normal velocity and acceleration to move about. However, when a player either travels up a slope, down a slope, or falls from the sky whilst they are using WASD E, the velocity is constant and therefore players do not go faster going down a slope, slower up a slope and the acceleration due to gravity is reduced when using these keys. Here is the local script I am using:

Character = script.Parent

Player = game.Players:GetPlayerFromCharacter(Character)

Torso = Character:FindFirstChild("Torso")

Humanoid = Character:FindFirstChild("Humanoid")

Camera = game.Workspace.CurrentCamera

repeat wait() until Player.Character ~= nil

Ball = script.Ball.Value

UIS = game:GetService("UserInputService")

Direction = {W = false, A = false, S = false, D = false}

Camera.CameraType = Enum.CameraType.Custom

Camera.CameraSubject = Ball

MaxVelocity = 35 -- Maximum marble speed

Acceleration = 2 -- Rate at which speed changes

--//Functions

function Parameters()

if Character and Torso and Humanoid and Ball then

	return true

else

	return false

end

end

function Reset()

Camera.CameraType = Enum.CameraType.Custom

Camera.CameraSubject = Humanoid

Ball:Destroy()

script:Destroy()

end

function HumanoidChanged()

Humanoid.Jump	= false

Humanoid.Sit = true

end

function Died()

Reset()

end

function UISDown(Instance, ProcessedEvent)

if ProcessedEvent then return end

if Instance.KeyCode == Enum.KeyCode.W then

	Direction.W = true

	while Direction.W do

		Ball.Velocity = Ball.Velocity + (Camera.CoordinateFrame.lookVector * Acceleration)

		wait()

	end

elseif Instance.KeyCode == Enum.KeyCode.A then

	Direction.A = true

	while Direction.A do

		Ball.Velocity = Ball.Velocity + ((Camera.CoordinateFrame * CFrame.Angles(0, math.rad(90), 0)).lookVector * Acceleration)

		wait()
	end	

elseif Instance.KeyCode == Enum.KeyCode.S then

	Direction.S	= true

	while Direction.S do

		Ball.Velocity = Ball.Velocity + (Camera.CoordinateFrame.lookVector * -Acceleration)

		wait()

	end	

elseif Instance.KeyCode == Enum.KeyCode.D then

	Direction.D	= true

	while Direction.D do

		Ball.Velocity = Ball.Velocity + ((Camera.CoordinateFrame * CFrame.Angles(0, math.rad(-90), 0)).lookVector * Acceleration)

		wait()

	end	

elseif Instance.KeyCode == Enum.KeyCode.E then

	Direction.W, Direction.A, Direction.S, Direction.D = false, false, false, false

	Ball.Velocity = Vector3.new(0, 0, 0)

end

end

function UISUp(Instance, ProcessedEvent)

if Instance.KeyCode == Enum.KeyCode.W then

	Direction.W = false

elseif Instance.KeyCode == Enum.KeyCode.A then

	Direction.A = false

elseif Instance.KeyCode == Enum.KeyCode.S then

	Direction.S = false

elseif Instance.KeyCode == Enum.KeyCode.D then

	Direction.D = false

end

end

function BallChanged()

if 	Ball.Velocity.X > MaxVelocity then  Ball.Velocity = Vector3.new(MaxVelocity, Ball.Velocity.Y, Ball.Velocity.Z)

elseif Ball.Velocity.X < -MaxVelocity then Ball.Velocity = Vector3.new(-MaxVelocity, Ball.Velocity.Y, Ball.Velocity.Z)

elseif Ball.Velocity.Y > MaxVelocity then Ball.Velocity = Vector3.new(Ball.Velocity.X, MaxVelocity, Ball.Velocity.Z)

elseif Ball.Velocity.Y < -MaxVelocity then Ball.Velocity = Vector3.new(Ball.Velocity.X, -MaxVelocity, Ball.Velocity.Z)

elseif Ball.Velocity.Z > MaxVelocity then Ball.Velocity = Vector3.new(Ball.Velocity.X, Ball.Velocity.Y, MaxVelocity)

elseif Ball.Velocity.Z < -MaxVelocity then Ball.Velocity = Vector3.new(Ball.Velocity.X, Ball.Velocity.Y, -MaxVelocity)

end

end

--//Logic

Humanoid.Changed:connect(HumanoidChanged)

Humanoid.Died:connect(Died)

UIS.InputBegan:connect(UISDown)

UIS.InputEnded:connect(UISUp)

Ball.Changed:connect(BallChanged)

1 comment

0

That's a lot of stuff there