So the method of collision detection is pretty simple. Create spherecast with same radius as sphere projectile in game. Spherecast distance needs to be however far the projectile is trying to move in that frame (i’m doing this all locally using runservice and deltatime). If the spherecast does hit something then move it based off of the spherecasts.Distance and do whatever you wanna do with objects that collide (in this case I just destroy the ball). If the spherecast does not catch anything then move the ball freely using its speed variable. Here’s what that code looks like:
local function MoveBall(dt, ball)
local zSpeed = BallSpeed*dt
local CollisionCheck = workspace:Spherecast(ball.Position, BallSize/2, Vector3.new(0,0,zSpeed), CollisionParams)
if CollisionCheck then
ball.Position = ball.Position + Vector3.new(0, 0, CollisionCheck.Distance*math.sign(BallSpeed))
ball:Destroy()
else
ball.Position = ball.Position + Vector3.new(0, 0, zSpeed)
end
end
RunService.RenderStepped:Connect(function(dt)
for i = 1, #Balls do
MoveBall(dt, Balls[i])
end
end)
I understand if the spherecast starts inside of the object then it won’t detect whatever it’s colliding with, but in this scenario how is that happening and how should I prevent it?
Shouldn’t it be moving right up to the wall but never inside of it? If I increase the speed of the balls then it seems to happen never so I do think that’s what is happening.