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)
We can use alignOrientation to orient the missile towards the target
To do that you must:
Create alignOrientation and connect to attachment
Mode of alignOrientation to Oneattachement
set angularVelocity torque and responsiveness to what you like. If moving to slow increase values
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.