How do I make a bullet raycast?

  1. What do you want to achieve?
    I want to have a bullet which will follow the Ray which is casted by the gun and also have a drop like Phantom Forces or other cool FPS games in Roblox.

  2. What is the issue?
    As I am new to the subject of Raycasting, I can’t figure out a way to do this.

  3. What solutions have you tried so far? I tried to find some solutions on the Dev forum, but I couldn’t understand much from that

Currently, I am using EgoMoose’s FPS Framework and I suppose I will have to make changes in the fire event, so theres the code for it.

function fps:fire()
	if (self.isEquipped and self.isAlive) and self.weapon.CurrentAmmo.Value > 0 then
		local from = self.isAiming and self.weapon.Aim or self.weapon.Shoot;
			
		local ray = Ray.new(from.Position, from.CFrame.lookVector*500);
		local hit, pos, normal = game.Workspace:FindPartOnRayWithIgnoreList(ray, {self.humanoid.Parent, self.weapon});
		
		self.weapon.Shoot.Flash.Enabled = true
		self.weapon.Shoot.Smoke.Enabled = true
		wait(.25)
		self.weapon.Shoot.Flash.Enabled = false
		self.weapon.Shoot.Smoke.Enabled = false
		
		self.weapon.CurrentAmmo.Value = self.weapon.CurrentAmmo.Value - 1		
		local GunUI = game.Players.LocalPlayer.PlayerGui.GunUI.Info		
		GunUI.Ammo.Text = self.weapon.CurrentAmmo.Value.."/"..self.weapon.MaxAmmo.Value

		
		local enemyHum;
		if (hit) then
			local players = game.Players:GetPlayers();
			for i = 1, #players do
				if (players[i].Character and hit:IsDescendantOf(players[i].Character)) then
					enemyHum = players[i].Character:FindFirstChild("Humanoid");
					break;
				end
			end
			if (enemyHum) then
				enemyHum:TakeDamage(self.settings.damageFunc())
			end
		end
		
		self.recoil.p = self.settings.recoilFunc();
		remotes.fire:FireServer(self.Right, self.Left, enemyHum);
		
		local serverWeapon = self.humanoid.Parent:FindFirstChild(self.weapon.Name)
		local fireSound = serverWeapon.Handle:FindFirstChild("Fire");
		if (fireSound) then
			fireSound:Play();
		end
	end
end
1 Like

The very general idea for bullet drop is this:

  • Get an equation that will return bullet position over time (you can find this on the internet, it’s a simple projectile equation)
  • Using that equation, update the bullet position on each frame. You’re basically raycasting from point to point until you hit something. The more frames you have, the more accurate the approximation of the bullet drop curve.

It gets more complicated when you work in using the server to verify what the client sees as hits and whatnot due to network latency but that’s a pretty quick overview.

2 Likes

This may be what you’re looking for.
There’s a tutorial on how to use it in the post.

1 Like