How to get a part to face at another part and move towards it

I was a little confused on the title, and you’ll be too. So I’ll explain better here with pictures.
This is my Rocket.
I need the rocket to reach the Red Block.

However, by doing:

Rocket.CFrame = Red.CFrame
It’ll end up looking like this: (it moves sideways there)
image
(I don’t mind it clipping through the Red Part, the Red Part is just there to Understand where visually where it’s aiming)

The desired outcome I’m looking for is this:
image

So basically whatever orientation the Rocket may be in, it’ll find its way and go to the spot facing the right way.

Thanks in advanced!

1 Like

This may work:

local part1 = ...;
local part2 = ...;
local speed = ...;
part1.CFrame = CFrame.lookAt(part1.Position, part2.Position)
part1.Velocity = (part2.Position - part1.Position).Unit*speed
1 Like

This is the simplest way in my opinion.

and after that code, put this:

local moving = true -- just a variable so you can make it stop moving

while moving do
  rocket.CFrame += rocket.CFrame.lookVector 
  wait(1/speed) -- speed here
end

Can this work if the Part is Anchored or only Unanchored?

Hey, That could work, but how would moving it go from there?
My original plan was to tween, but I’ve realized that the speed will be different every time basically

It would not work if the part is anchored, but if it is anchored there is a workaround:

local part1 = ...;
local part2 = ...;
local speed = ...;
part1.CFrame = CFrame.lookAt(part1.Position, part2.Position)
part1.Position = part1.Position + part1.CFrame.lookVector*speed

Are you sure it wouldn’t work if the part was anchored?

The initial solution I suggested wouldn’t work because applying a Velocity on an anchored object would have no effect whatsoever.

1 Like

Ah, I misunderstood. I thought you were talking about setting it’s CFrame.

1 Like

There are many ways you can move it… maybe tweens, bodymovers, look vectors like I mentioned, and even lerping!

1 Like

Tweens would be the best imo, because you can pause them and all that. You can check when they complete, also.

But, if you’re going to do them over and over again, you might want to use a different method, as they can be very laggy.

CFrame is position and orientation value of an object,
and really useful according to my experience with it, here an example for its uses

if you want to make the part go to a certain position using CFrame -

local rocket = game.worksapce.rocket
rocket.CFrame = CFrame.new(1,1,1) -- The rocket will go to this position 
-- imagine it like vector3.new(1,1,1)

And if you want rotate it with CFrame -

rocket.CFrame = CFrame.Angles(math.rad(90),0,0) -- it will rotate to 90 with x axis (must use math.rad)

if want it to face another part like in your case -

local anotherPart = game.workspace.part

rocket.CFrame = rocket.CFrame * CFrame.new(rocket.Position,anotherPart.Position)
-- as you can see the first parameter is the where the rocket should be positioned,And the second parameter is where the rocket should looking at and both must be vector3 values

I hope i Helped you,its my first time at the Devfurom (:

Hey, for BodyGyro I believe the part needs to be unanchored sadly, so I don’t think it’ll move, that is unless there’s a way to keep it from falling with BodyGyro?

Hey, I tried that but it seemed to note move still sadly

Can you share the modified code that you have tried?

Also tried this, which works pretty well but it’s very very choppy.

Which I also believed you said tweening if there’s many wouldn’t be a good idea? There will be quite a few so I don’t think I’d be able to tween to fix the choppyness?

Yeah, here

mouse.Button1Down:Connect(function()
local pos = mouse.Hit.Position
local x = Instance.new("Part",workspace)
x.Position = pos
local Missle = Missles:Clone()
Missle.Parent = workspace

Missle.CFrame = CFrame.lookAt(Missle.Position, pos)

local Speed = 10

Missle.CFrame = CFrame.lookAt(Missle.Position, pos)
Missle.Position = Missle.Position + Missle.CFrame.lookVector*Speed

end)
``

As a disclaimer I did remove some parts (parts that are irrelevant here) just in case some things look a little off etc.

This is what the outcome is whenever I do it, the white part represents where it should go:
image

1 Like

You would have to constantly update the position in order for this to work, as whatever the code is doing is it repositions the parts some studs towards the other part.

Ah I gotchu, I gotchu.

would TweenService be able to do this without causing any problems with multiple happening ( this wouldn’t be on the server btw, it’ll be client )?

Hey I tried it but for whatever reason BodyGyro just spins it instead of moving it anywhere, I did check which faces are for the Rocket, but the Front adds up with the Front