I would like to make some ball projectiles able to hit the target and explode right in the exact position in which they hit the target. I have the damage part down fine, and even added some knock-back, but every time I throw one of the balls, it doesn’t explode where the part actually touched and explodes some place before.
Here’s a video of what I mean:
https://gyazo.com/77d6f5b784d140977a7984308beb57b5
As you can see, sometimes the positions are right, but most of the time it hits somewhere before where the ball touches.
Here’s the part in my script that throws the balls (roo = the ball btw):
elseif toDo == "Throw" then
local ball = char.LocalRoo:FindFirstChild(player.Name.."'s Roo"..amount)
ball.Parent = workspace
if amount == 1 then
rightHand:FindFirstChild("RooWeld"..amount):Destroy()
elseif amount == 2 then
leftHand:FindFirstChild("RooWeld"..amount):Destroy()
else
root:FindFirstChild("RooWeld"..amount):Destroy()
end
-- Removing Old Body Velocities
for i,v in pairs(ball:GetChildren()) do
if v.ClassName == "BodyVelocity" then
v:Destroy()
end
end
-- Creating New Body Velocity
ball.Anchored = false
local bv = Instance.new("BodyVelocity",ball)
bv.Name = "Flying"
bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bv.Velocity = (ball.Position-mouseHit).Unit*-150
ball.Name = "RooAttack"
local touch
touch = ball.Touched:Connect(function(hit)
if hit.Parent == char or hit.Parent.Parent == char then return end
touch:Disconnect()
-- Creating Explosion
local explosions = {}
local amount = 3
for i = 1,amount do
local explosion = ball:Clone()
explosion.Name = "Explosion"
explosion.Anchored = true
explosion.Material = Enum.Material.Neon
table.insert(explosions,explosion)
end
-- Adjusting Explosions
local vary = ball.Size.X
for i,v in pairs(explosions) do
-- Positioning
v.CFrame = CFrame.new(ball.Position + Vector3.new(math.random(-vary,vary),math.random(-vary,vary),math.random(-vary,vary)))
v.Parent = workspace
-- Getting Tweens
local info = TweenInfo.new(
0.5,
Enum.EasingStyle.Sine,
Enum.EasingDirection.InOut,
0,false,0
)
local properties = {
Size = v.Size*3;
Transparency = 1
}
local tween = ts:Create(v,info,properties)
tween:Play()
-- Damage
local density = ball.Density.Value
local touched = {}
v.Touched:Connect(function(hit) damage(player,hit,touched,density,false,ball) end)
end
-- Cleaning Up
ball:Destroy()
wait(0.5)
for i,v in pairs(explosions) do
v:Destroy()
end
end)
elseif toDo == "Burst" then
I looked around on the Developer Hub but I couldn’t find anything about the .Touched having positions lag behind, and also wasn’t sure what to search up to find anything like that. I also tried simply using ray-casting to find the position I’d need the ball to explode at, but it always seemed to go off in some random direction completely ignoring the mouse. I’d post about the ray-casting issue instead, but I feel like there should be some easy way to just used .Touched for this.
Edit: When I lower the strength of the body velocity, this doesn’t occur as much, so I think the issue might lie in the fast speeds of the projectile. Either way, I just need some workaround.