Raycast Weapon Projectiles

I always think of making projectiles for guns with raycasting, I currently use BodyVelocity for moving… But I want to use raycasting for projectiles, but I dont know how I would do that, I dont want to use FastCast since I want to try stuff on my own. But could someone explain how I would do this?

5 Likes

Essentially what you need to do is a “stutter step” raycast. Raycast a certain number of studs based on the time since you last did a raycast and the velocity of the projectile, with a set maximum length per cast to allow for bullet drop or other physics interactions. After each raycast, update the velocity/acceleration/whatever and continue!

All this being said, even if you don’t use FastCast, it could be a fantastic resource to figure out parts of this problem, as I believe it does something similar to what I suggest.

2 Likes

I asked someone else tho, it works! I added bullet drop to mine tho. I will just mark urs as solution.

2 Likes

How did you figure it out and do bullet drop?

I just raycast every .Stepped:Wait() and move the bullet forward a bit
and for bullet drop i just make it lower on the y axis everytime it moves it forward a bit and make the orientation different

1 Like

This is exactly what I suggested with my “stutter step” method!

I found out I need deltatime filtering for my bullets, how would I do that?

here is a part of what i currently have for the bullets on the clients

function movebullet(Bullet, Direction, StepDistance, Damage, Character, Tool)
	local ReturningBool = false
	
	Bullet.Position = Bullet.Position + (Direction * 6)
	Bullet.Position = Bullet.Position - Vector3.new(0,.1,0)
	Bullet.Orientation = Bullet.Orientation - Vector3.new(.09,0,0)
	
	local RayRaycastParams = RaycastParams.new()
	RayRaycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	RayRaycastParams.FilterDescendantsInstances = {Bullet, Character, Tool, lastBullet}	
	local ray = workspace:Raycast(Bullet.CFrame.Position, Direction * StepDistance, RayRaycastParams)
	if ray then
		ReturningBool = true
		local humanoid = ray.Instance.Parent:FindFirstChildOfClass("Humanoid")
		if humanoid then
			GunDamageRemoteEvent:FireServer(humanoid, Damage)
		end
		Bullet:Destroy()
	else
		ReturningBool = false
	end
	return ReturningBool
end


	local maxdistance = 300
	local stepdistance = 3
	for times = 1, math.floor(maxdistance/stepdistance) do
		game:GetService("RunService").Stepped:Wait()
		if movebullet(Bullet, Bullet.CFrame.LookVector, stepdistance, 6, Character, Tool) == true then break end
	end
	if Bullet then
		Bullet:Destroy()
	end

7 Likes

You could try to get a scale of the deltatime by doing game:GetService(“RunService”).Stepped:Wait()*30 and multiply the position and direction by that. Also, getting the runservice for every time a bullet moves is very inefficient and would be better to have as a variable, and you should make the raycast params a argument aswell instead of creating a new one every single time.