Missiles using CFrame won't look towards their target

reposted from yesterday since I couldn’t get a definitive answer past a dead end.

Alright so basically I’m trying to make these cool missiles for a boss fight I’m making, a homing one and a not homing one, with functions as you’d expect.

should be simple except that said missiles have to be moved with CFrame due to how they were constructed ( direct position changes will stop welded parts from being moved with it ), it has a central MovementPoint part which is moved using CFrame in order to move the missiles

using CFrame for this comes with a problem though, rotating the missiles towards a target point can’t be done within a Tween , at least not very easily.

when a CFrame tween is used on the missiles, it will make them rotate vertically during it, ending with vertical missiles instead of them facing the target. I have since corrected this and instead now they can end up in all sorts of directions based on the method used which is still very much not what I want

I’ve tried all sorts of methods to get the tweens to work along with the lookAt from manual CFrame position changes through While loops, CFrame.lookAt(), CFrame.Angles(), Orientation changes and so on. basically everything I could think of, yet none of it worked.

I feel providing most of the script and what it does might be handy, therefore I will. keep in mind no errors show up

script.Parent.Event.Event:Connect(function(Target)
	local Thing1 = (script.Parent.MovementPoint.Position - Target).Magnitude
	local Thing2 = (Thing1 / 0.5) / 100
	local TweenInf = TweenInfo.new(Thing2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
	
	local Finish = {CFrame = CFrame.new(Target)}

	local Tween = TweenS:Create(script.Parent.MovementPoint, TweenInf, Finish)
	Tween:Play()
	wait(Thing2)
end)

there’s a little bit more to it but it’s irrelevant here
Target is the target position, it isn’t an object or instance, it is a Vector3 value.
Thing1 and Thing2 are pretty self-explanatory, they just judge the distance from the start to the target to make the missile speed consistent.

the rest is pretty obvious.

I’m at a complete loss for what to do and I’m very stuck right now. any decent advice that doesn’t require me to completely redesign the missiles or do some drastic over-the-top math calculation would be much appreciated.

Update: I have fixed the rotation of the Homing missiles by rotating their MovementPoint in a specific way, however Non-homing missiles are still not quite working

this edit was fairly different from the original version because it didn’t really make sense anymore past a certain point

1 Like

Can I see the whole script to check for errors?

You should do it inside a RenderStepped/Heartbeat connection, you can use while x do loops if you want, then you need to set up a variable to save its last position(vectir3), the length to predict is next position(a number) and the direction where it will go.(vector3, ie CFrame.LookVector)
Inside the loop you have to do:

-- this code is tested
Heartbeat:Connect(function()
  local vel= direction * length
  local newpos = lastpos + vel -- * (deltaTime)
  missile.CFrame = CFrame.new(lastpos, newpos)
  lastpos = newpos
end)

you can detect if it hits anything using raycasts

1 Like

Hmm. Süß. If you don’t know how CFrame.lookat works it works like this.

The first parameter is the Position where the CFrame will be located. The second parameter shows the target where the CFrames forward position will point at. If you put both where it is and where it will look to you will certainly run into problems. I suspect the resulting CFrame is NaN.

To fix this the first parameter is where you want the missile to move forward to. For example
Missile.Position + Missile.CFrame.LookVector gets a forward point exactly 1 stud Infront of the missile.

Second is that I don’t recommend you use tweens UNLESS you know where the target is going to be before the missile hits. If you have a moving object or if you missile will be accelerating or turning midflight I recommend a different method.

There are 2 other methods. The easiest is the constraints/body mover based one where you attach a mover to the missile and move it that way.

The second is to CFrame the missile every frame. You will get much better results with this method but it is quite hard to setup depending on how much functionality you want with it. You will use stepped event and update the CFrame every frame.

no errors to find, the part that’s faulty is the tween itself

I’ll try this since this actually looks like it would work

they need to be able to look at moving targets.

I don’t fancy trying my luck with BodyMovers since setting those up would probably be more trouble than it’s worth considering you also gave me an answer which would probably work much better even if the setup time would be a bit consuming.

Can you show a video/gif of the issue before you did the attempted fix that sent it flying 99999999999 studs away?
I think that would help us understand your problem a lot better

there’s not a lot to show really, the missiles spawn and get an event fired to them to make them start moving, after which the tween immediately sends them flying downward to that Y axis in about 1-2 frames.

in other words it’s so fast that there’s literally no footage I could feasibly capture or show

the results were uhhh odd to say the least, they rotate towards the player however they have a very drastic offset which makes them completely vertical as opposed to horizontal

I should probably clarify even more since I completely forgot to mention this, the boss shoots two types of missiles:
Homing and Non-Homing

Homing missiles need to constantly move towards the player and look at them

Non-Homing missiles need to look at a single target point and move towards it

both of them can move fine it’s the looking at the target that’s the issue

I would just poll for the current slope of the path theyre traveling on

Each time you move it, save the previous position and do this:

missile.CFrame = CFrame.lookAt(currentPosition, 2 * currentPosition - previousPosition)

If you need to use CFrame.Angles to rotate it you can just do

CFrame.lookAt(...)*CFrame.Angles(math.rad(blah),0,0)

Or whatever else you need to do

no better result, anything I have tried has still either resulted in the missiles being sent into the void, uncontrollably rotate or just not move at all to begin with.

edited the original post since it’s already kinda outdated and really needed a rewrite for better context