I am trying to make a ricocheting projectile. I have created a part and I think I have correctly created a ray that reflects. The problem is I don’t know how to move the part along the ray that I have created. local startCFrame = bullet.CFrame
local normal = startCFrame.lookVector
local position = startCFrame.p
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {sP}
local ray = Ray.new(position, normal * 500, raycastParams)
local hit, position, surfaceNormal = game.Workspace:FindPartOnRay(ray) --Cast ray
if (hit) then
--Get the reflected normal
local reflectedNormal = (normal - (2 * normal:Dot(surfaceNormal) * surfaceNormal))
--Set new normal
normal = reflectedNormal
--bullet.BodyVelocity.Velocity = normal * sP.Speed.Value
bullet.BodyVelocity.Velocity = normal * sP.Speed.Value
end
--Create reference to a BindableEvent
local triggerBullet = Instance.new("BindableEvent")
triggerBullet.Event:Connect(function()
local stepped
stepped = game:service "RunService".Stepped:connect(function()
if bullet ~= nil and bullet.Parent == sP then
bullet.CFrame = bullet.CFrame + bullet.CFrame.LookVector * 20/40
else
stepped:Disconnect()
end
end)
end)
triggerBullet:Fire()
Instead of moving the projectile like the way in the code above I want to move it using the ray so when Part moves far enough that it collides with something it will bounce off.
local startCFrame = bullet.CFrame
local normal = startCFrame.lookVector
local position = startCFrame.p
-- Set an origin and directional vector
local rayOrigin = bullet.Position
local rayDirection = bullet.CFrame.LookVector
-- Build a "RaycastParams" object and cast the ray
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {sP}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local hit, position, surfaceNormal = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if hit then
local hitPart = hit.Instance
-- Check if the part resides in a folder, that it's fully visible, and not locked
if hitPart.CanCollide == true then
--Get the reflected normal
local reflectedNormal = (normal - (2 * normal:Dot(surfaceNormal) * surfaceNormal))
--Set new normal
normal = reflectedNormal
--bullet.BodyVelocity.Velocity = normal * sP.Speed.Value
print("HIT")
--bullet.CFrame = normal * sP.Speed.Value
end
end
--Create reference to a BindableEvent
local triggerBullet = Instance.new("BindableEvent")
triggerBullet.Event:Connect(function()
local stepped
stepped = game:service "RunService".Stepped:connect(function()
if bullet ~= nil and bullet.Parent == sP then
bullet.CFrame = bullet.CFrame + bullet.CFrame.LookVector * 20/40
else
stepped:Disconnect()
end
end)
end)
triggerBullet:Fire()