#Moving Parts with WASD

1 messages · Page 1 of 1 (latest)

quartz rune
#

Trying to create a couple of parts to move when I use my move keys, heres the scipt

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local player = game.Players.LocalPlayer

local function getCity()
return workspace.Cities:FindFirstChild(player.Name .. "_City")
end

local speed = 40
local turnSpeed = 1.2

local forward = 0
local turn = 0

UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then forward = 1 end
if input.KeyCode == Enum.KeyCode.S then forward = -1 end
if input.KeyCode == Enum.KeyCode.A then turn = -1 end
if input.KeyCode == Enum.KeyCode.D then turn = 1 end
end)

UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.S then forward = 0 end
if input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D then turn = 0 end
end)

RunService.RenderStepped:Connect(function(dt)
local city = getCity()
if not city then return end

local core = city.PrimaryPart
if not core then return end

local move = core.CFrame.LookVector * forward * speed * dt
local rotate = CFrame.Angles(0, turn * turnSpeed * dt, 0)

core.CFrame = core.CFrame * rotate + move

end)

dawn trail
#

this is some nice script but I think you could've wrapped it in some OOP, and an array for the key bind something like this
local Forward = {
[Enum.KeyCode.W] = 1,
[Enum.KeyCode.S] = -1
}

local Side = {
[Enum.KeyCode.D] = 1,
[Enum.KeyCode.A] = -1
}

local moveX = 0 -- sideways
local moveZ = 0 -- forward/backward

UserInputService.InputBegan:Connect(function(input, gp)
if gp then return end

if Forward[input.KeyCode] then
    moveZ += Forward[input.KeyCode]
end

if Side[input.KeyCode] then
    moveX += Side[input.KeyCode]
end

end)

UserInputService.InputEnded:Connect(function(input, gp)
if gp then return end

if Forward[input.KeyCode] then
    moveZ -= Forward[input.KeyCode]
end

if Side[input.KeyCode] then
    moveX -= Side[input.KeyCode]
end

end)

it will be way cleaner

#

yeah maybe mine is just overkill

leaden raptorBOT
#

studio** You are now Level 7! **studio

junior vine
dawn trail
junior vine
#

its like building a whole ass mansion for animals to live in

#

cherry on top the animals are ants

#

@chilly ice is this your alt acc

dawn trail
#

well he want an object to move

#

so if he want more OOP can do so

#

simple just call

#

like if he has more than 1 object

junior vine
#

hm yeah youre jax's alt acc

dawn trail
#

nop I am my main account

#

who is jax?

chilly ice
#

arent you the guy that tried to tell someone not to use OOP in a textbook example of it lol

#

i dont know why you would use oop for this though

junior vine
#

6 days