How do I make a code that moves a part forward if the player holds down the W key

The post/topic’s name isn’t just about me wanting to know how to make a part move forward when you are pressing down the W key, it’s a bit more complicated than that, so please read everything and I tried making it as easy to understand as possible but giving as much info as possible

I’m trying to make a horror game where you mainly crawl in small tunnels
The code works and you “crawl” in the tunnels, what I mean by that is that the player isn’t even inside the tunnel because the player is actually on top of the tunnel while a part with the name CameraPart is inside the tunnel and it’s welded to the player’s HumanoidRootPart
and the player’s CurrentCamera’s CameraSubject is the CameraPart to make it look like the player is inside the tunnel
and the reason why is because no matter how much I have tried searching on google and devforum
I still haven’t found a way to make the CameraPart move forward when you press W without having to make the player’s character move
and when the tunnel starts going up I want the camera to look up but after 30 minutes or so of trying to find a solution by searching on google and devforum
I gave up and tried guessing my way but that just ended up with only the CameraPart looking up but the camera remained in the exact same orientation and didn’t follow the orientation of the CameraPart

Sorry for the long explanation, this is my first time posting on devforum because I really want to make this game but cannot do it because I literally have no knowledge in the area of programming cameras

but here’s a screenshot to make it easier to understand what I mean:
Screenshot 2022-12-20 173948

And here’s the code:

wait(1) --Wait 1 second so the player has time to load to prevent errors
workspace.CurrentCamera.CameraSubject = workspace.CameraPart
wait(0.1)
local HumanoidRootPart = script.Parent.Parent.Character:FindFirstChild("HumanoidRootPart")
workspace.CameraPart.WeldConstraint.Part1 = HumanoidRootPart -- Welds the HumanoidRootPart to CameraPart
wait()
workspace.CameraPart.Anchored = false --Unanchors the CameraPart so the player can move

And this script is for preventing the player from being able to walk to the sides so the player can’t fall off

local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(inst) --Runs the code below if any key is pressed
	if inst.KeyCode == Enum.KeyCode.W or --Checks if the W key is the key being held down
		inst.KeyCode == Enum.KeyCode.S then --Checks if the S key is the key being held down
		script.Parent.Parent.Character.Humanoid.WalkSpeed = 5 --Allows the player to move if either W or S key is being held down
	else
		script.Parent.Parent.Character.Humanoid.WalkSpeed = 0 -- If neither the W key or S key is being held down, freeze the player
	end
end)
uis.InputEnded:Connect(function(inst) --Runs the code below if it detects a key has stopped being held down
	if inst.KeyCode == Enum.KeyCode.W or --Checks if it was the W key that was stopped being held down
		inst.KeyCode == Enum.KeyCode.S then --Checks if it was the S key that was stopped being held down
		script.Parent.Parent.Character.Humanoid.WalkSpeed = 0 --Freezes the player if either the W or S key isn't being held down anymore
	end
end)

Again, this is the first time posting on devforum so please tell me if there’s anything else I gotta include in the post
and

2 Likes

I’m not sure of the exact setup, but if you use CFrame.lookAt and point it toward the mouse position the player can control the camera with the mouse, the same as using the Right mouse button.
I know there are other posts here about aiming the camera with the mouse.

I’m sure others will have ways to make the camera look up or down if the tunnel is sloped, but this is just an idea of what can be done.

2 Likes

If the camera part is welded, it must be moving with the humanoid root part

Wait, you have assigned the Part1 to the HumanoidRootPart which makes it secondary, like then it won’t care that if you are moving or not, assign the Weld0 to HumanoidRootPart because it just works like a primary part in a model !

Oh sorry I forgot to mention that I want the player to not be able to move the camera and the camera moves by itself when it has to, for example when you go in a tunnel that goes up the camera rotates so it looks up to make the player unable look around to give a claustrophobic feeling

What do you mean? The CameraPart moves wherever the player moves so there’s nothing wrong with the weld constrait

A raycast downwards from the camera Part can determine the Normal of the surface it instersects. Is it Possible to get the Normal Vector of a Surface? - #3 by DevSynaptix
You may be able to aim the camera depending on the Normal of the part below it.
RaycastResult | Roblox Creator Documentation

1 Like

Your post mentions that you want your camera part moving whenever your player moves, I also read your explaination on that, can you explain further though because if this isn’t your problem, then what’s the problem

Hmmm, do you want to make your camera move but player doesn’t ?

Yes exactly! That’s what I meant, but I don’t know how to make it go up when it has to go up like when a tunnel starts going up, instead of it just noclipping through the tunnel and just keeps moving forward, it automatically goes up

I don’t really understand what you mean but when you started talking about raycasting I remembered I can maybe make the part move upwards if it detects the part under it is getting closer to it,
but I still don’t how to make the camera rotate without the player having to move the camera with the RBM button, because in the horror game I want the player to get disoriented and forget what’s really up or down, so if I make the camera move the amount of degrees the tunnel is going up, it will feel like you are still going forward and not up
So I wanna know how I can make the camera move without the player moving it, I have managed to make it work using two parts and making the camera part look at an invisible part in front of it, but as soon as I start to move, the camera is still stuck in the same place but the camerapart still moves

Then I don’t really understand why to weld it with the character if you don’t want that when the player moves the camera part doesn’t

Hmmm…I don’t really know about how to make it go up when the player doesn’t. Sorry

Okay, but do you know how I can rotate the CurrentCamera, I have tried doing that and it worked, but it’s really confusing because it’s facing away from the tunnel even tho the Camera Part is facing toward the tunnel and it’s orientation is 0,0,0 which means the CurrentCamera shouldn’t face away from the tunnel when its at 0,0,0 and when I tried making it face the tunnel by making it turn 180 degrees
it only turns 90 degrees

If you read the information you’ll see that you can get the Normal result (the Orientation of the surface) from the Raycast.
You can tilt the camera in relation to the Normal.
And as you said, the Raycast can return the distance to the Part below it and raise up or lower to keep it the same height above the Part.

I’m not sure how you’d move the camera without moving the player.

I’ve seen other posts about making the player into a custom character (think of marble games where you become the marble) and you can then control the movements using custom controls.

Hehe, I just thought that for the disorientation effect you wanted to create you could switch the control inputs, like switch left and right controls after jumpscares etc.

1 Like

That last part sounds actually like a good idea