Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is this an efficient way to handle bullet travel on the server?

Using raycasting every frame on the server, I'm aware this completely ignores the size of the bullet. If I wanted to incorporate that would it be wise to just unanchor the bullet and use velocity with a touch listener? Or I could incorporate EgoMoose's Region3 module. Any ideas?

---@param origin CFrame
	---@param whitelist table<int, Instance> 
	function Class:Launch(origin, whitelist)
		local start = tick()
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = whitelist or {}
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		raycastParams.IgnoreWater = true
		
		local result = nil
		local yVelocity = 0
		while tick() < start + self.Lifetime and not result do
			local step = RunService.Stepped:Wait()
			local gravity = (workspace.Gravity / 196.2) * 9.807
			local drop = (gravity / 2) * math.pow(step, 2)	--drop = gravity/2 * t²
			
			yVelocity = (yVelocity + drop)
			local target = (origin * CFrame.new(0, 0, -self.Speed * step)) - Vector3.new(0, yVelocity, 0)
			local rayResult = workspace:Raycast(origin.Position, (target.Position - origin.Position).Unit * (self.Speed * step), raycastParams)
			
			if rayResult then
				self.Projectile.CFrame = CFrame.new(rayResult.Position, target.Position)
				if rayResult.Instance then
					result = rayResult
				end
			else
				self.Projectile.CFrame = CFrame.new(target.Position, origin.Position) * CFrame.Angles(0, math.rad(180), 0)
			end
			
			origin = target
		end
		
		print("Projectile took", tick() -start, "seconds")
		
		if result then
			print(result)
			local surface = self:GetSurface(result)
			print(surface)
		end
	end

2 answers

I would personally use a BodyPosition with a touched event to handle this, you can probably also use the touched event with the tween service which is easier to work with. Raycasting is better in order to predict a target rather than for collisions

1 comment

0

I don't think the touched event fires when the projectile is anchored, also a BodyPosition is not good for this at all

Your script doesn’t work because the function is not local.

This discussion is preserved as it was. New answers, comments, and votes are closed.