I’m trying to get a projectile to hit a wall and bounce, and it’s pretty much working, but one problem I’ve noticed is that the projectile clips through a bit because of how the raycasting works.
I tried subtracting the part size from the end destination, but that only works if the part’s moving forward relative to the world, and in other cases it just fully passes through.
I don’t have much knowledge on CFrames and Vector3s, but here’s the current code that I’m using:
local function projectileLaunch(projectileName : string, front : BasePart, rotationalVector : Vector3, plrChar : Model, projectileParams : table)
local excludeFilter = {}
local newLerpPos
local lookDirect
local clonedProjectile : Model = ServerStorage:WaitForChild(projectileName):Clone()
local hitbox : BasePart = clonedProjectile:WaitForChild("Hitbox")
clonedProjectile.Parent = game.Workspace
clonedProjectile:PivotTo(front.CFrame * CFrame.new(front.CFrame.LookVector * -5))
local beginPosition = clonedProjectile:GetPivot()
print(projectileParams)
lookDirect = clonedProjectile:GetPivot().LookVector
for i = 1, projectileParams.MaxTargets do
print(lookDirect)
table.insert(excludeFilter, clonedProjectile)
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
for _, Player in ipairs(Players:GetPlayers()) do
table.insert(excludeFilter, Player.Character or Player.CharacterAdded:Wait())
end
rayParams.FilterDescendantsInstances = excludeFilter
local rayCasted = game.Workspace:Raycast(clonedProjectile:GetPivot().Position, lookDirect*2000, rayParams)
if rayCasted then
print(rayCasted.Instance)
print(rayCasted.Instance.Parent)
--newLerpPos = CFrame.new(rayCasted.Position - Vector3.new(hitbox.Size.X/2, 0, hitbox.Size.Z/2))
newLerpPos = CFrame.new(rayCasted.Position)
local mag = (newLerpPos.Position - beginPosition.Position).Magnitude
for i = 0, 1, (projectileParams.Speed/mag)/60 do
clonedProjectile:PivotTo(beginPosition:Lerp(newLerpPos, i))
task.wait()
end
lookDirect = lookDirect - 2*(lookDirect:Dot(rayCasted.Normal))*rayCasted.Normal
beginPosition = newLerpPos
end
table.clear(excludeFilter)
end
Debris:AddItem(clonedProjectile, 0)
end