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
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.
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.
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)