Help with moving at an angle

Hi, I was developing a game about moving a submarine and then quickly ran into a problem. The problem is that if the orientation is not 0,0,0 then it won’t move in the correct direction. Here’s an example of the problem:

How can I fix this?

1 Like

What’s the script?

clicked = false
z = 0
function c()
	if clicked then
		script.Parent.Text = "Not-activated"
		clicked = false
	else	
		script.Parent.Text = "Activated"
		clicked = true
	end
	
	if clicked then
		
		while wait(0.01) do
			z += 0.1
			game.Workspace.Part.Position = Vector3.new(0,8,z)
		end

	end
	
end

script.Parent.MouseButton1Click:Connect(c)

The reason this is happening is because Position is not relative to the part you’re moving. Think of the baseplate as a giant x, y, z axis, and that you’re moving the part along that. No matter which orientation you have it’s going to follow the same axis, in which your case in the Z-axis.

To fix this, you’ll have to use CFrame which will move the part on it’s own axis. The Z-axis will move the part forwards or backwards, X-axis will move the part left or right, and the Y-axis will move the part up and down.

local part = workspace.Part --Identify the part before-hand so it's not constantly getting indexed in the loop. 
while wait() do
	part.CFrame = part.CFrame * CFrame.new(0 ,0, -.1) --Negative for forward, Positive for backwards
end
2 Likes

Thank you so much for the help. :grinning:

1 Like

No problem, I really recommend you get comfortable with CFrame as you will use it a lot throughout your scripting journey. For starters, you can read through this Understanding CFrames

1 Like

Hey, thanks.
I was able to implement it very easily. But I tried to do the same for a second part and this happened:

I’m guessing it is because of the different sizes of the parts. But I am not sure.