Missile does not spawn correctly when jet is at high speeds

I am working on a jet system for a game of mine.

The jet has a decent missile spawning/launching system when it is flying at a reasonable speed, as seen below:

However, when the jet goes really fast, the missiles spawn really far behind the jet, as seen below:

How can I fix this? This is the below code that is used to spawn missiles:

local missileSpawn = weapons:FindFirstChild("spawn")
			if not missileSpawn then return end
			local missileClone = script.missile:Clone()
			missileClone.Parent = workspace
			missileClone:SetPrimaryPartCFrame(missileSpawn.CFrame)
			--missileClone.PrimaryPart.CFrame = missileSpawn.CFrame+(missileSpawn.CFrame.LookVector*10)
			missileClone.PrimaryPart:SetNetworkOwner(plr)
			if not target then --- free fire missiles
				local BodyVelocity = Instance.new("LinearVelocity",missileClone.PrimaryPart)
				BodyVelocity.Attachment0 = missileClone.PrimaryPart.fly
				BodyVelocity.MaxForce = math.huge
				BodyVelocity.VectorVelocity = missileClone.PrimaryPart.CFrame.LookVector*(speed*2)
				local GunBodyGyro = Instance.new("AlignOrientation",missileClone.PrimaryPart) 
				GunBodyGyro.Mode = Enum.OrientationAlignmentMode.OneAttachment
				GunBodyGyro.Attachment0 = missileClone.PrimaryPart.fly
				GunBodyGyro.MaxTorque = math.huge
				GunBodyGyro.CFrame = CFrame.new(missileSpawn.CFrame.LookVector,missileSpawn.CFrame.LookVector*2)
			else --- heat seeking
				hub.createHomingMissile(missileClone.PrimaryPart,target,speed)
			end

All I can suggest is to set the missiles cframe.position at the last possible moment before parenting it since your jet changes position so rapidly. Perhaps even compensating for the speed of the jet by offsetting the projectile at launch.

EDIT: I think also setting the missiles collision group should fix the misslie colliding with the jet.

1 Like

I ended up doing this:

if speed > 250 then
				missileClone.PrimaryPart.CFrame = missileSpawn.CFrame+(missileSpawn.CFrame.LookVector*(speed/7))
			else
				missileClone.PrimaryPart.CFrame = missileSpawn.CFrame+(missileSpawn.CFrame.LookVector*(speed/15))
			end

offset will depend on how fast the jet is going

1 Like

Add the Jet.AssemblyLinearVelocity/60 when you spawn the missled.

1 Like

You can always say multiply the position by the speed * constant (constant being 1.2 or something). This way no matter what the speed is the missile will have an offset smaller for slower speeds and larger for faster. You can then just test a few values until you’re happy with the result.

1 Like

As for the CollisionGroups, setting that didn’t really help either.

1 Like

This isn’t being caused by collission… It’s being caused because of lag. That’s why I mentioned to use Velocity to compensate for the lag.

1 Like

Can you explain how that will help with the lag?

1 Like

Basically, till the event is fired and the missle is spawned, the player who is handling the jet will move some distance, so as we give the server our OLD position it would basically lag behind the jet, now as we know the jet is travelling at some velocity i.e Jet.AssemblyLinearVelocity. So by adding this to the object’s position, we can get the position of the jet in future time (relative to the event fire time) i.e where it is now. Make sure to divide it by the FPS of the client i.e deltaTime returned by the RunService.RenderStepped function to properly compensate or else the missle will go 400 or more studs infront of the jet.

2 Likes

hi friend,

one thing you can do is:

local origin = missilespawn.Position

local DistanceInfront = 5

local target = nil --u can use lookvector if ur just moving it straight
local aim = (target-origin).unit --Returns the lookvector from the spawn to the target
local spawnPos = origin + ( aim * DistanceInfront) --spawn pos is origin + aim

--[[ you can multiply lookvector to spawn it further infront 
(this has the y included so it will also go up 5 but you can remove if u want 
and multiply by only x and z) ]]

local targetPos = spawnPos + aim --we want missle to look at the spawn pos + the aim

Missile..CFrame=CFrame.new(spawnPos,targetPos)
Missle.Velocity = aim*--whatever u wanna multply it by it contrls the speed

basically missle cframe is set to the missle spawn position + the aim * however far u want infront
then its velocity is set to the aim position * however strong u want it to be.

This did not really change the way the missile(s) were spawned. Can you see the code I posted in my initial post and tell me what line(s) I should get rid of, that might interfere with your suggested line?

This won’t work, sometimes the jet fires a guided missile at someone, sometimes it free fires a regular missile.

Couldn’t you spawn it welded onto the plane and then add the velocity when it’s unwelded?

I do that for flares, and it has the same issue.

set the target to either position? the rest of the math works out
also why r u setting network ownership of the missle to the plr thats why its lagging