How do I make a part move in the direction it is facing?

I have a bird that flies forward, turns around and flies back, the problem is whenever I want to rotate that bird to fly in a different direction I have to go into the script and adjust it for the new direction, I would like to have the script take the direction the bird is facing and automatically make it fly in that direction and then back.

local root = script.Parent.Pigeon

local speed = 0.2

while wait(0.1) do

root.Orientation = Vector3.new(0,-90,0)
for i = 1,500, 1 do
	root.CFrame = root.CFrame + Vector3.new(speed,0,0)
	print(i)
	wait(0.01)
end
root.Orientation = Vector3.new(0,90,0)
for i = 1,500, 1 do
	root.CFrame = root.CFrame + Vector3.new(-speed,0,0)
	print(i)
	wait(0.01)
end

end

6 Likes

You may also want to read up on tweening for much smoother movement:

2 Likes

You could use LookVector
That would look something like this

root.Orientation = Vector3.new(0,90,0)

for i = 1,500, 1 do

root.CFrame = root.CFrame + (root.CFrame.LookVector * Vector3.new(speed,0,0))

print(i)

wait(0.01)

end
14 Likes

This is just going to make the bird move in the X axis.
If you want to make it move forwards, you need to use LookVector.

root.CFrame += root.CFrame.LookVector * speed
2 Likes

its very simple: you just need to get a variable with the value of the orientation of the part,
then you simple change the parts position according to the variable. although this may seem complitcated we can simplify it enough to find a solution form a kids programing language such as scratch!
you know the move block on scratch? ex. move () steps.
well this actually has to do with this problem ur having, the block works like this:
it takes the direction of the sprite and does math to calculate the x and y variables from the digrees of the sprint and those variables to move the sprite in the direction its facing, however that is still very complex but we can use the fact this is not block coding to our advantage.
so we get the x orientation and the y orientation to calulate the motion on the 2d level and you just do the same with z and boom problem solved! im sorry if this solition is confusing because it is hard to explain but now that you have a somewhat idea of how it works. i would show you the code but first i need some context, are you trying to move in the sky or on the ground?

1 Like

although i have solved this problem before, my memory is not good enough to give you some exact code or vairiables. but at least i could give you an explanation of what you need to do and gave you some ideas on how to do so

1 Like