So what I want is to have a missile (model) move up several hundred studs in the air, then rotate so its tip is pointing at a part named "Target" somewhere below, then move to that target. I have no problem with the moving up part, but I don't know how to make the missile rotate to face the "Target" and then move towards it. I was thinking of doing something with MoveTo(), but will that work?
How can I make a missile look at and move to a part?
1 answer
Nikkulai is probably on the right track; the problem here is (to some extent) is that CFrames represent points in a set of data, where you have a curve (and roblox's body physics can be garbage to work with). What you want is the opposite of curve fitting; so, to use CFrames (probably the easiest option) you would have to sample your path at a given interval and update your object's CFrame.
Think like this; (move a part along a parabola in 2d space)
local trajectory = function(i, t)
-- where i is the initial height (studs), and t is time (seconds)
return 1/2*-1*t^2+([[exit velocity*sine or cosine of exit angle]])t+i
end
for a = 1, 10 do
print(trajectory(0, a))
end
local initial = game.Workspace.Part.Position
for a = 1, 10 do
game.Workspace.Part.Position = initial + Vector3.new([[exit velocity*sine or cosine of exit angle]]*a, a, 0);
end
-- calculate the change in x, y for 10 points (1, 2, 3, ..., 10 seconds) and add it to the initial position
This becomes very complex in 4 dimensions (x, y, z, time)
Nikkulai's point of prosperity: When specifying a new CFrame with CFrame.new(); CFrame.new(Vector3 position, Vector3 look) will create a new CFrame at position, looking in the direction of look.
Really, you'll probably want to calculate a path from the missile's initial position to it's final position accounting for the missile's angle of exit and exit velocity; this is simple enough in 2d space for an object on which the only force is gravity-
h(t) = (gravity*t^2)/2 + exitvelocity*math.sin(exitangle) + initialheight
Now find a path for a missile with a motor producing thrust in it's local upward/forward direction in global 3d space over time
That's a start - all untested pseudocode and theory, use it as you desire
2 comments
Wow, this is so much more complex that what I came up with. I'm using just Nikkulai's model:SetPrimaryPartCFrame(CFrame.new(model.PrimaryPart.Position, part.Position)) and then moving it forwards using lookVector. I never though of doing it with an arch. However, with my scripting experience level, I doubt I will be using things like this anytime soon
Haha, whatever works. There's no right way to design; answered more for my fun than your help.
This discussion is preserved as it was. New answers, comments, and votes are closed.

6 comments
model:SetPrimaryPartCFrame(CFrame.new())
But how will this help? This just teleports the rocket to the position you type into CFrame.new(). I use this method to move the rocket up, but I can't see how you can use this to move the rocket toward a certain part
you could try something like model:SetPrimaryPartCFrame(CFrame.new(model.PrimaryPart.Position, part.Position)) and then use lookvector in a force to move it towards it.
okay... I have no idea how to do that (I'm not a pro scripter), but ill try to research it and try it. Thanks
Also, should I use a BodyThrust or a BodyForce or something else?
This is a complex problem, so I should warn you that you'll need a pretty decent background in mathematics (particularly geometry - lots of geometry - and algebra, if not some calculus) and Lua. And kinematics too, of course, if you're looking to play with ballistics. I'll try and come up with something of an example as I can.