I am making a first person shooter which is pvp. For my weapons, i had an older system which instantiated a part and gave it a velocity. This system however, was laggy and had hit detection problems. I then heard of another widely used method, which is firing the clients to locally make a part and give it a velocity (which would be for show) and then the server would iterate over the bullet’s path, making rays every few increments and checking for parts on that ray.
I have been trying to implement this, but i am not having much luck. Im pretty lost when it comes to understanding this. The code seems to give the projectile drop, but for some reason does not produce the correct direction
I have this code segment right here:
Functions (@tyridge77 gave me these, i did not write them):
local function CalculateStartVelocity(pos, goal, power, accuracy) local maxAngle = math.rad(125)*math.clamp(1 - accuracy, 0, 1) return (CFrame.new(pos, goal)*CFrame.Angles(0, 0, math.random(0, 2*math.pi))*CFrame.Angles(math.acos(math.random(math.cos(maxAngle), 1)), 0, 0)).LookVector*power end local function CalculateProjectilePosition(startpos, startvelocity, timeSince,gravity) return startpos + startvelocity * timeSince + 0.5 * gravity * timeSince ^ 2; end
local CurrentPosition = pos.p local LastPosition = CurrentPosition local StartVelocity = CalculateStartVelocity(pos.p, pos.LookVector*2, speed, 10) print(StartVelocity) for i = 1, 50 do wait(0.01) CurrentPosition = CalculateProjectilePosition(LastPosition, StartVelocity, (0.1 * i), Vector3.new(0, workspace.Gravity * bolt:GetMass() * bolt.Drop.Value, 0)) local ray = Ray.new(LastPosition,CurrentPosition-LastPosition); local beam = Instance.new("Part", workspace) -- so i can see the path beam.CanCollide = false beam.Anchored = true beam.Material = "Neon" beam.Position = LastPosition LastPosition = CurrentPosition end