From a physics perspective, velocity is a direction and speed, direction is a unit vector, and speed is a number. You stated velocity = direction*speed then later multiplied velocity by speed again. Remove the second time you have shotVel[1] (which is speed).
local ray1 = Ray.new(oldpos, velocity)
I don’t know if you were trying to make a realistic arc or not. If you do as I suggested, you just have to make sure your units line up. It helps if do this consistently which would also mean knowing the time for each step and adding it to your calculation.
Here is an example of how I would accomplish this.
local speed = 30 --studs per second
local acceleration = Vector3.new(0, -98.1, 0) --studs pre second per second
local timeLimit = 10 --In case the ray is shot into the void
local ignore = Instance.new("Folder", workspace)
function drawBeam(startPos, endPos)
local beam1 = Instance.new("Part")
beam1.BrickColor = BrickColor.new("Cyan")
beam1.FormFactor = "Custom"
beam1.Material = "Neon"
beam1.Transparency = 0.25
beam1.Anchored = true
beam1.Locked = true
beam1.CanCollide = false
local distance = (endPos - startPos).magnitude
beam1.Size = Vector3.new(0.3, 0.3, distance)
beam1.CFrame = CFrame.new(startPos, endPos) * CFrame.new(0, 0, -distance / 2)
beam1.Parent = ignore
end
local rayCastParams = RaycastParams.new()
rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist
rayCastParams.FilterDescendantsInstances = {ignore, }
function throw(startPos, targetPos)
local velocity = (targetPos - startPos).Unit*speed
local lastPosition = startPos
local timePos = 0
local connection
connection = game["Run Service"].Heartbeat:Connect(function(dt)
timePos = timePos + dt
if timePos >= timeLimit then
timePos = timeLimit
connection:Disconnect()
--No parts were hit in the time limit. Place C4 anyway?
end
local newPosition = startPos + velocity*timePos + 0.5*acceleration*timePos^2
drawBeam(lastPosition, newPosition)
local result = workspace:Raycast(lastPosition, (newPosition - lastPosition), rayCastParams)
if result then
connection:Disconnect()
--Plant C4 at result.Position
end
lastPosition = newPosition
end)
end
wait(2)
throw(Vector3.new(0,20,0), Vector3.new(10,20,0))