Hi - we have a script we use that can make a turret shoot at a character. Problem is, it can’t hit a character that is running across it’s vision (laterally). It is always a stud or two behind the character. If the character stops running, or runs directly toward the turret, the turret can hit. We are using this code to print “l” the laser bolt (it’s a basepart)
man, this is a small treasure trove of stuff I haven’t learned to do yet. the code is really elegant too. Thanks, I’ll dig into this on Monday/Tuesday and if it works I will hit you the “solution” badge.
This is a really good video and youtube channel in general - really appreciate the reference.
So this video shows what occurs if I set the target to the characters HumanoidRootPart.
I have a print that tells what the projectile .Touched On the visible misses, it almost always shows an object it “hit”. (“UpperRightArm” or “HumanoidRootPart” or “LeftFoot”)
So it is somehow hitting these things - but visibly it doesn’t seem to be ?
This video shows what happens (cancollide is true on projectile) Sometimes it doesn’t even make visible contact with a character part, it just bounces off an invisible hitbox?
:
The video below is when “cancollide” is false:
while true do
target = nil
task.wait(2)
local position1 = workspace.From.Position
for i,v in pairs (workspace:GetChildren()) do
if v:FindFirstChild("Humanoid") then
target = v:FindFirstChild("HumanoidRootPart")
end
end
if target then
local position2 = target.Position
local direction = position2 - position1
local duration = math.log(1.001 +direction.Magnitude * 0.01)
position2 = target.Position + target.AssemblyLinearVelocity * duration
direction = position2 - position1
local force = direction / duration + Vector3.new(0, game.Workspace.Gravity * duration * 0.5, 0)
--
local clone = game.ServerStorage.Projectile:Clone()
clone.Position = position1
clone.Parent = workspace
local touchedAlready = false
clone.Touched:Connect(function(touchedPart)
if touchedPart.Name ~= "From" then
if touchedAlready == false then
touchedAlready = true
print(touchedPart)
end
end
end)
--
clone:ApplyImpulse(force * clone.AssemblyMass)
clone:SetNetworkOwner(nil)
end
end
The code is from the youtube you linked to, Suphi Kaner (really worth watching too - thanks!). I adjusted it to fire at the character’s humanoidRootPart, instead of at an object in Workspace called “To”.
The “touched” part is also my addition, it prints what the block hits. When I check the server to see what it’s been hitting, it often states a character part - even when it appears to miss.
I think you copied the script wrong. You put Position2 after instead of before.
Script:
while true do
task.wait(3)
local target = nil
local position1 = workspace.From.Position
for i,v in pairs (workspace:GetChildren()) do
if v:FindFirstChild("Humanoid") then
target = v
end
end
if target then
local rootPart = target:WaitForChild("HumanoidRootPart")
local humanoid = target:WaitForChild("Humanoid")
local position2 = rootPart.Position
position2 = rootPart.Position + rootPart.AssemblyLinearVelocity
local direction = position2 - position1
local duration = math.log(1.001 + direction.Magnitude * 0.01)
local force = direction / duration + Vector3.new(0, workspace.Gravity * duration * 0.5, 0)
local clone = game.ServerStorage.Projectile:Clone()
clone.Position = position1
clone.Anchored = false
clone.Parent = workspace
local debounce = false
clone.Touched:Connect(function(hit)
if hit.Name ~= "From" then
if not debounce then
debounce = true
end
end
end)
clone:ApplyImpulse(force * clone.AssemblyMass)
clone:SetNetworkOwner(nil)
end
end
Use a laser and make the turret a bullet. This will never work with physics because the player on their client is always ahead of their actual position on the server. Make a direct raycast to its targets. For effects, use a beam, and make the beam attach from an attachment on the turret, to an attachment on the player’s character. Beams update to the exact position of the player. Turning FaceCamera on will make it look almost identical to a cylinder. If you want a cannon ball, make it explode with an area of effect that would affect the character.
I have to admit its working a little better…But now it typically fires in front of the character, and still misses.
However, it works well enough for what we are doing, and I like it more than the result we were getting before.
A strange thing I noticed, when Suphi replots position 2,he does it because he wants to replot after calculating “duration” with the logarithmic. He needs the first “position2” so he can plot the duration, then he includes " * duration" in the replot of position2…so that the linearvelocity is multiplied by the length of time the bullet will travel.
I notice in your version you remove this. But when it comes down to it, it seems to work better without this line. ?