I am attempting to create a car script which runs on variable numbers. When a key such as W,A,S,D is pressed it will change a variable value from either 0 to -1 or to 0 to 1 (Depending on direction) so that the characters vehicle will move forwards. I wish to make this movement less janky though, so how could I make it so when a key is pressed the variable gradually increases until it is either 1 or -1. Any help or information would be much appreciated.
I do not understand, so when you press a key, then a numbers goes up but the max is 1?
Yes, this is how the script works, when a key is pressed it changes its assigned variable number to a max of either 1 or -1 depending on what key is pressed. (I have taken forwards and backwards out for now) I wish to make this varaible number change a gradual change.
local speed = 2
Direction = {upForce = 0, forwardForce = 0, backwardForce = 0, leftForce = 0, rightForce = 0}
userInput.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.A then
Direction.leftForce = -1
elseif Input.KeyCode == Enum.KeyCode.D then
Direction.rightForce = 1
end
end)
local Movement = game.Workspace.CurrentCamera.CoordinateFrame:VectorToWorldSpace(Vector3.new((Direction.leftForce)+(Direction.rightForce),math.abs(Direction.forwardForce)*.2,Direction.forwardForce+Direction.backwardForce))*speed
Wait why cant you just use key down? Then script the movement
Just add a simple line,
userInput.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.A then
if Direction.leftFoce <= 1 then
Direction.leftForce = -1
elseif Input.KeyCode == Enum.KeyCode.D then
if Direction.rightForce >= 0 then
Direction.rightForce = 1
end
end
end)
No, this is not what needs to be changed. I just need a script so that when Eum.KeyCode.A is pressed the direction.leftForce is gradually changed from 0 to 1 in an increment. So every .1 of a second it would add +0.1 to the 0 so that after a second it would be equal to 1. it would not go above 1.
I think you should just try and use UserInputService.InputBegan, changing a variable from false to true somewhere, then in a loop - increasing the number until it either reaches 1 (at which point, it would do nothing anymore) or the variable changes to false.
Yes this was my question from the start, what loop could I use to make the variable number from “Direction” gradually increase from 0 to 1, instead of it changing to 1 instantly.
The question is how do I gradually increase the variable number value from “Direction”, gradually so that is dosnt instantly become 1.
Try this:
local holding = false
local speed = 2
Direction = {upForce = 0, forwardForce = 0, backwardForce = 0, leftForce = 0, rightForce = 0}
userInput.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.A then
holding = true -- holding, so increment is available
while holding do -- while available do
Direction.leftForce = Direction.leftForce + 0.1 --- number of increment
wait(1) --- time between each increment
end
end
end)
userInput.InputEnded:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.A then
holding = false -- not holding A anymore, end increment
end
end)
Is there also anyway I could tween this movement from 0 to 1?
I never use tweens so I’m not sure - probably yes, but I guess if you made the wait(1) something akin to wait(0.05) the increment would be way smoother.
edit: also I forgot to add a limit for when the increment reaches 1, but I’m pretty sure you can figure that out (just replace “while holding do” with “while holding and not (Direction.leftForce >= 1) do”)
Hi,
I don’t think Tweening is necessarily the right way forward. The above provides a valid enough solution, except try adding a constraint onto the while loop:
while Value < 1 do
Value = Value + 0.1;
wait(1);
end
This means that if the value hits the end of the range, it won’t increase any more. By using this method, you’re creating a constant acceleration (or a constant second differential of displacement to be more technical) so an accelerating tween would accelerate the acceleration, which isn’t what you want in a vehicle without it looking really weird.
I hope this helps,
-Tom