# Help with custom Control Script

Problem 1
So basically, i have 2 variables, hsp and vsp. These values can either be 1 or -1
And i put them in a vector3 like this

local direction = Vector3.new(hsp,0,vsp)

This is basicly just 8 direction movement! (Method stolen from GameMaker tutorials)
But the only problem with this is when hsp and vsp are the same, so like if hsp = 1 and vsp = 1 thats moving down and to the right its 2 times faster than just hsp = 1.

In unity theres a thing called Normalize? i think? Im not really familier with unityâ€¦ Iv only used it like 4 times. But basicly the normals in unity makes it so the diagonal movement is the same speed as the normal movement.

Is there something like this in Roblox? Or is there a good method to do thisâ€¦
Heres my movement code as it is now.

---up and down
if InputObject.KeyCode == Enum.KeyCode.W then
if inputState == Enum.UserInputState.Begin then
moving_up = 1
elseif inputState == Enum.UserInputState.End then
moving_up = 0
end
end
if InputObject.KeyCode == Enum.KeyCode.S then
if inputState == Enum.UserInputState.Begin then
moving_down = 1
elseif inputState == Enum.UserInputState.End then
moving_down = 0
end
end
---left and right
if InputObject.KeyCode == Enum.KeyCode.D then
if inputState == Enum.UserInputState.Begin then
moving_right = 1
elseif inputState == Enum.UserInputState.End then
moving_right = 0
end
end
if InputObject.KeyCode == Enum.KeyCode.A then
if inputState == Enum.UserInputState.Begin then
moving_left = 1
elseif inputState == Enum.UserInputState.End then
moving_left = 0
end
end
if is_grounded == true then
ysp = 0
vsp = (moving_down - moving_up) * PLAYER_SPEED * 2
hsp = (moving_right - moving_left) * PLAYER_SPEED * 2
else
ysp = -PLAYER_SPEED
vsp = (moving_down - moving_up) * PLAYER_SPEED
hsp = (moving_right - moving_left) * PLAYER_SPEED
end
player.Character.HumanoidRootPart.LinearVelocity.MaxForce = PLAYER_SPEED * 10
player.Character.HumanoidRootPart.LinearVelocity.VectorVelocity = Vector3.new(hsp,ysp,vsp)

also ysp is just the y movement.
If all that doesnâ€™t make sense at all to you i have another problemâ€¦

Problem 2
Is there a way to set the MAX SPEED of a LinearVelocity, Yes theres a MaxForce property but no matter how i change it the player still gains speed as it walksâ€¦

It starts fine then the longer the LinearVelocity moves in one direction the faster it gets. And thats not really ideal. Or should i be using some other force to move the player character?

EDIT: just fixed both problems

1 Like

One thing you could try is to limit the Magnitude of the velocity. Magnitude is speed.

Example:

if(velocity.Magnitude > maxSpeed)then
velocity = velocity.Unit * maxSpeed --Limit the speed/magnitude without changing the direction.
end
1 Like