I would like to have the player constantly move forward as if they were pressing W while still being able to jump, move left and right.
Also they wouldn’t be able to go backwards obviously.
I looked everywhere on the internet not just devforum but not a single thread talks about anything like this. Using vectors technically works but it disables player input, meaning I can’t move left and right.
Here’s the script that has the vector system:
local Humanoid = script.Parent.Humanoid
game:GetService("RunService").RenderStepped:Connect(function()
Humanoid:Move(Vector3.new(0,0,-1), true)
end)
Can you make a custom control script which doesn’t read the W and S inputs for character control and just set it so the forward input is constantly true?
It works but it only moves in one direction and doesn’t change directions even when I move the camera. Pressing A and D still doesn’t let me go left and right.
So basically with this I would still be exactly where I started.
I think the problem here is the granularity/resolution of the movement function (RenderStepped) is beyond the granularity/resolution of the UserInputService. You would have to check the players input on RenderStepped and apply adjustments on a lower granularity/resolution. Maybe disable actual character movement and adjust it based on the UserInputService prompts.
This is the only thing mentioning exactly what I would like to achieve on all of google that I could find but I have no clue what this guy means when he describes what he did and he also didn’t tell how he did it in a script.
I’m pretty sure they meant the script should be in StarterCharacterScripts, not the LinearVelocity.
Make the script create a LinearVelocity with the proper value for forward movement and parent it to the HumanoidRootPart of the character.
I’m gonna write this in pseudo code so its up to you to translate but this is essentially what you want to do:
local MoveX = 0
--functions to detect input and change moveX accordingly
function KeyDown(key)
if key == "A" then
MoveX += -0.5
elseif key == "D" then
MoveX += 0.5
end
end
function KeyUp(key)
if key == "A" then
MoveX -= -0.5
elseif key == "D" then
MoveX -= 0.5
end
end
OnKeyDown:Connect(KeyDown)
OnKeyUp:Connect(KeyUp)
game:GetService("RunService").RenderStepped:Connect(function()
Humanoid:Move(Vector3.new(MoveX,0,-1), true)
end)
Again, just pseudo code and not exactly the cleanest method of doing it but hopefully this helps you understand the gist of it.
Wow thanks that actually makes so much more sense. I didn’t know there was a way to have that kind of setup in a vector3 system. I tought you could only write numbers in there.
I’ll try something like this out and reply with the results.
Well yes, that is still the case. The variable ‘MoveX’ is always going to be a number so thats why we use it there. Understanding how variables work is one of the absolute fundamentals of coding.
Oh you want it to move towards camera, thats simple too, just make it lookvector of camera cframe, and I’m not sure why people are making it complicated here
Try this:
local rs = game:GetService("RunService")
local camorientationpart = Instance.new("Part",workspace)
camorientationpart.CanCollide = false
camorientationpart.Transparency = 1
local character = script.Parent
local function changecframe()
camorientationpart.CFrame = game.Workspace.CurrentCamera.CFrame
character:WaitForChild("Humanoid"):Move(camorientationpart.CFrame.LookVector*1)
end
rs:BindToRenderStep("Camera",Enum.RenderPriority.Character.Value,changecframe)
This isn’t pretty efficient, you shouldn’t advise someone to use such a long script for small task, and the input when the player moves camera is not w,a,s or d, its camera movement, he was asking if you can go like this:
I did this same result with this code. This is not what I wanted to achieve. What I wanted to achieve was what @jbjgang2 helped with in a very efficient way, AKA being able to move in the camera’s direction WHILE ALSO being able to move left and right with D and A.
local Humanoid = script.Parent.Humanoid
game:GetService("RunService").RenderStepped:Connect(function()
Humanoid:Move(Vector3.new(0,0,-1), true)
end)
I had a friend on discord write this code for me and finished it at the exact same time when I started getting replies here and it works perfectly. Thanks everyone for your help, and for those who have the same problem here’s the code. I got permission to post it.
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local aKey = Enum.KeyCode.A
local dKey = Enum.KeyCode.D
local function onInputBegan(humanoid)
if UserInputService:IsKeyDown(aKey) == true then
humanoid:Move(Vector3.new(-0.6,0,-1), true)
elseif UserInputService:IsKeyDown(dKey) == true then
humanoid:Move(Vector3.new(0.6,0,-1), true)
end
end
RunService:BindToRenderStep("move", Enum.RenderPriority.Character.Value + 1, function()
if player.Character then
local humanoid = player.Character:FindFirstChild("Humanoid")
if humanoid then
wait(0.04)
humanoid:Move(Vector3.new(0, 0, -1), true)
onInputBegan(humanoid)
end
end
end)
I also wrote out a working version of the pseudo code I sent you @IvanDani75, this one is a bit cleaner and should work better when addressing some edge cases than what your friend wrote for you.
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local keys = {
["A"] = 1; --left
["D"] = -1; --right
}
local moveX = 0
UserInputService.InputBegan:Connect(function(InputObject)
if InputObject.UserInputType == Enum.UserInputType.Keyboard then
for key, value in pairs(keys) do
if key == InputObject.KeyCode.Name then
moveX += 0.6 * value
end
end
end
end)
UserInputService.InputEnded:Connect(function(InputObject)
if InputObject.UserInputType == Enum.UserInputType.Keyboard then
for key, value in pairs(keys) do
if key == InputObject.KeyCode.Name then
moveX -= 0.6 * value
end
end
end
end)
game:GetService('RunService').RenderStepped:Connect(function(dt)
if player.Character and player.Character:FindFirstChild('Humanoid') then
player.Character.Humanoid:Move(Vector3.new(moveX, 0, 1))
end
end)