Hello! I need help with making an object bounce when it hits something. I need to use bodyVelocity but idk how to convert it. Please help. Code:
local function Shoot(hrp,mouseCF,spellName)
local rng = Random.new()
local laser = Instance.new("Part")
laser.CFrame = hrp.CFrame
laser.Parent = workspace
local maxDistance = 200
local curDistance = 0
local stepDistance = 4
local stepWait = 0
local currentPos = hrp.Position
local currentNormal = (mouseCF - hrp.Position).lookVector
local function Step(overrideDistance)
-- Cast ray:
local params = RaycastParams.new()
local direction = currentNormal * (overrideDistance or stepDistance)
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {hrp}
local result = workspace:Raycast(currentPos, direction)
local pos
if result then
pos = result.Position
else
pos = currentPos + direction
end
-- Update laser position:
laser.Size = Vector3.new(0.4, 0.4, (pos - currentPos).Magnitude)
laser.CFrame = CFrame.new(currentPos:Lerp(pos, 0.5), pos)
local oldPos = currentPos
currentPos = pos
if result then
-- r = d - 2(d DOT n)n
-- Reflect:
print(result)
local norm = result.Normal
local reflect = (currentNormal - (2 * currentNormal:Dot(norm) * norm))
currentNormal = reflect
Step(stepDistance - (pos - oldPos).Magnitude)
return
end
curDistance = (curDistance + (pos - oldPos).Magnitude)
-- Apply fade effect to laser as it approaches max distance from < 75 studs:
if curDistance > (maxDistance - 75) then
local d = (curDistance - (maxDistance - 75)) / 75
laser.Transparency = d
end
-- Recurse if max distance not reached:
if curDistance < maxDistance then
wait(stepWait)
Step()
end
end
Step()
-- Done! Destroy laser:
laser:Destroy()
print("DESTROY")
end
Thanks!