Why do this missile keep glitching?

Im trying to make so that a missile moves towards some dummies (for example)

The problem is that it glitches, it just teleports back for some reason.
https://gyazo.com/00c43335827a1af6ddc7c0210b41eb22

local function cframeLookAt(pos, lookAt)
    local look = (lookAt - pos).Unit
    local right = look:Cross(Vector3.new(0, 1, 0))
    local up = right:Cross(look)
    return CFrame.fromMatrix(pos, right, up, look)
end

while true do
	local EndPos = workspace[FollowName].HumanoidRootPart.Position
	local TweenI = TweenInfo.new((Missile.Position-EndPos).Magnitude/Speed, Enum.EasingStyle.Linear)
	local TweenIO = TweenInfo.new(0.1, Enum.EasingStyle.Linear)

	local TweenPlay = TS:Create(Missile, TweenI, {Position = EndPos})
	local TweenOplay = TS:Create(Missile, TweenIO, {Orientation = Vector3.new(cframeLookAt(Missile.Position, EndPos))})
	TweenPlay:Play()
	TwenOPlay:Play()
	wait(0.1)
	TweenOplay:Cancel()
	TweenPlay:Cancel()
end

EndPos is the position it tries to move towards

I don’t think using tweening is a good way to make a rocket.

Instead try using bodyMovers (Don’t ask me this stuff I don’t actually good at physics)

Secondly, if you really want to use TweenService then do

TweenPlay.Completed:Wait()

Like that instead of using a fixed value like .1 second

The reason i dont use .Completed that is cause the endpos may change, and i want it to follow the target.

Hold up why are you using a loop to do your tween??

As i said, the endpos may change at any time, since i want the missile to follow the target, so i need to make a new tween that moves it to the new position

Firstly, calling :Cancel() on a TweenObject will reset the variables you are trying to tween and it doesn’t look like it’s necessary considering the tween will only play for 0.1 seconds.

Having said that, you really should consider using bodymovers as they have support built-in. You can always try RocketPropulsion.

Well I never make a homing missle before so I don’t know what is the best way to do this. I have been searching about this on Roblox’s community but it doesn’t work.

I would suggest you to find videos on how homing missles actually works though. Then you will figure out a better code or some kind of like that.

Personally, I think your code is not good at performance, since it literally used a while loop and it isn’t fun if your game has multiple homing missles running.

I tried removing the Orientation thing, and it worked, it moved smoothly. It just didnt rotate it, which i want it to do.

I want to use tweening for it, i’m not planing to use it in a game, I just want to try and challenge me to do it with tweens.

then I think remove tween:Cancel() would be fine.

I tried that, the exact same thing happend.

In that case, I would suggest that you tween the CFrame of the model as opposed to Orientation and position simultaneously.

It looks like your cframeLookAt function returns a CFrame and is then converted to a Vector3. I believe that will take the positional component of CFrame and so you appear to be trying to set Orientation to that position. Also, I think Orientation takes it’s angles as YXZ instead of the usual XYZ so if you do keep using be mindful not to give it incorrect angles.

The simplest way would probably be to tween the CFrame to something like CFrame.new(Position, endPos) and skip the two tweens entirely.

1 Like

The reason i don’t use CFrame is cause i want the orientation to be complete before the position.

If you really want to do both separately, you will still need to pass the correct angle to the Orientation. Try using :ToEulerAnglesYXZ( ) to get the orientation from your CFrame returned by cframeLookAt.
(That function returns Y,X,Z)

There is no such thing as ToEulerAnglesYXZ

Atleast not in vector3 or cframe

Did you mean fromEulerAnglesYXZ ?

CFrame:ToEulerAnglesYXZ( ) does exist?

Oh, i didn’t see those : in front of it, sorry

It still errors saying that it does not exist.

    return CFrame:ToEulerAnglesYXZ(pos, right, up, look) 

It is a function of the datatype CFrame. You need to apply it to the CFrame you already have:

local Y,X,Z = cframeLookAt(pos, lookAt):ToEulerAnglesYXZ()
local orientation = Vector3.new(Y,X,Z)

oh like that, i tought you meant in the other function