Make missile fly where my mouse is pointing at

Hello,

So as the title says I need help with making a missile that is controlled by the player’s mouse.

I have in total 3 scripts 2 in the missile model and 1 in StarterPlayer.

This is the script that gives the explosion:

local Terrain = false
local flying = false
local readyLaunch = true

local maxHeight = 100
local aboveTarget = Vector3.new(-122,100,-120.25)

local uis = game:GetService("UserInputService")

local nuke = script.Parent.Parent -- verander dit naar script.Parent en plaats het script in een Part of iets anders wat je ook maar wilt zolang het in de workspace zit!

local terrain = workspace.Terrain



wait(2)
flying = true


wait(2)
			
for i, v in pairs(nuke:GetChildren()) do
	
	v.Touched:Connect(function(hit)
		if hit.Name == "Terrain" then

			print("YES")
	
						
			local currentPosition = nuke:GetPivot()
			local explosion = Instance.new("Explosion")
			explosion.Parent = nuke
			explosion.Position = nuke.PrimaryPart.CFrame.Position
					
			explosion.BlastRadius = 100000
			explosion.BlastPressure = 50000
			explosion.TimeScale = 0.25
							
						
			wait(0.1)
			nuke:Destroy()
		end
		
	end)
end
	

And this is the script that is attachted with the LocalScript in StarterPlayer:

print("step -4")

local velocity = 100

local rep = game:GetService("ReplicatedStorage")

local uis = game:GetService("UserInputService")
local remote = rep:WaitForChild("RemoteEvent")


print("step -3")
local currentCamera = workspace.Camera



local nuke = game.Workspace.Nuke
print("step -3.5")


			
game.Players.PlayerAdded:Connect(function(player)
	print("step -2")
	remote:FireClient(player)
	print("step 2")
end)




And this is the script that I need help with so the player can control the missile:

local rep = game:GetService("ReplicatedStorage")
local remoteEvent = rep:WaitForChild("RemoteEvent")
local uis = game:GetService("UserInputService")
local nuke = game.Workspace.Nuke

local player = game:GetService("Players").LocalPlayer

local mouse = player:GetMouse()


remoteEvent.OnClientEvent:Connect(function()
	print("step 2")
	uis.InputChanged:Connect(function(input, engine_processed)
		print("step 3")
		
		if input.UserInputType == Enum.UserInputType.MouseMovement then
			print(engine_processed)
			local bp = Instance.new("AlignPosition")
			bp.ApplyAtCenterOfMass = true
			bp.MaxVelocity = 50
			bp.Responsiveness = 200
			bp.Mode = Enum.PositionAlignmentMode.OneAttachment
			bp.Parent = nuke.NukeProfile
			bp.Position = Vector2.new(mouse.X, mouse.Y)
			print("step 5")
		end		



	end)
end)

And the LocalScript doesn’t works.

1 Like

I would recommend tweeting the missile to the location and after the tween is done explode it
(rocket launcher style)

or:

Control it with wasd keys

No, I like it better if they can just control it with mouse. If you can help me that would be great!

Then I would recommend having the player go into first person on the missile and move the missile in the direction of the mouse is in

Ok, but that doesn’t makes the rocket fly and control the missile with the mouse.

You’ve made so many posts about this one thing, just bump your other post(S) instead of creating a new one

Yes, but in the other posts they don’t respond. And I really want to know how to fix this.

Ok so far the control script has this but still isn’t working can someone help please?

local rep = game:GetService("ReplicatedStorage")
local remoteEvent = rep:WaitForChild("RemoteEvent")
local uis = game:GetService("UserInputService")
local nuke = game.Workspace.Nuke

local player = game:GetService("Players").LocalPlayer

local mouse = player:GetMouse()

local runService = game:GetService("RunService")

local bg = Instance.new("AlignOrientation")
bg.Parent = nuke.NukeProfile

local attachment = Instance.new("Attachment")
attachment.Name = "Attachment0"
attachment.Parent = nuke.NukeProfile

		
	
bg.Name = "AllignOrientation"
bg.Attachment0 = attachment



local ap = Instance.new("AlignPosition")
ap.Parent = nuke.NukeProfile
ap.Name = "AlignPosition"
ap.Responsiveness = 0
ap.MaxForce = Vector3.new(0,0,0)
ap.Position = Vector3.new(0,0,0)

uis.InputChanged:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		print("mouse has moved")
		
		local mouseP = mouse.Hit.Position
		
		ap.Mode = "OneAttachment"
		ap.Position = Vector3.new(mouseP.X, mouseP.Y, mouseP.Z)
		ap.Responsiveness = 30
		
		
		
		
		
	end
end)

@OverDayLight how do I make it so the rocket can fly?

1 Like

Okay. Unfortunately it seems you are approaching this the wrong way.

Rockets are a moving object with a velocity. Instead of using alignPosition you should be using LinearVelocity.

To get the rocket to fly forward these are the steps.

  1. Create attachment in rocket
  2. Create linear velocity
  3. Velocity set to (0,0,-X) where X is the speed you want your rocket to fly at(don’t forget negative)
  4. Max force on linear velocity. We need force to move your rocket
  5. RelativeTo attachment0 Rocket move forward depending on way it is facing.
    https://gyazo.com/a46eab38946e47993826e5e66607ba7e

We can use alignOrientation to orient the missile towards the target
To do that you must:

  1. Create alignOrientation and connect to attachment
  2. Mode of alignOrientation to Oneattachement
  3. set angularVelocity torque and responsiveness to what you like. If moving to slow increase values
  4. In a script during task.wait or Runservice.Heartbeat or stepped you need to tell the orientation to look at the target. Like this
while true do
	task.wait()
	AlignOrientation.CFrame = CFrame.lookAt(Missile.Position,Target.Position)
end

https://gyazo.com/65e4d2345288a5f4a2f2244caedbf997
You can get the target position by raycasting using the ViewportPointToRay Camera method.
as you can see for the missile movement you do not need a lot of code. This is because of the new constraints offer much better way of controlling your objects.

5 Likes

Well, this is actually exactly what I was hoping for not full scripts so I can learn it myself. But things to start somewhere!

1 Like