Issue with New Raycasting

hello guys im yeildng and im currently using the new update raycasting and make a gun out of this but i dont know how to explain the problem but i have some image of the problem and the code

this is the images



so i want to shoot the bullet go to the(green thing / attachment ) but the bullet go to another position

heres the code


local barrel = script.Parent
local ball = workspace.ball
local att = script.Parent:WaitForChild("Attachment1")

local params = RaycastParams.new()
params.FilterDescendantsInstances = {script.Parent.Parent:GetChildren()}
params.FilterType = Enum.RaycastFilterType.Blacklist
params.IgnoreWater = true

local deb = game:GetService("Debris")

while wait() do
	
local raycastResult = workspace:Raycast(barrel.Position,barrel.CFrame.LookVector * 20,params)

	if raycastResult then	
		att.WorldPosition = raycastResult.Position
		local newpart = Instance.new("Part")
		newpart.Parent = workspace
		newpart.BrickColor = BrickColor.Black()
		newpart.Shape = Enum.PartType.Ball
		newpart.Size = Vector3.new(0.4, 0.4, 0.4)
		newpart.CFrame = barrel.CFrame
		
		local vel = Instance.new("BodyVelocity",newpart)
		vel.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		vel.Velocity = raycastResult.Position
		deb:AddItem(newpart,1)
	else
		warn("no hitted")
	end
end

so that the script

someone can give me a tips to make a gun using raycasting

Remember that velocity is a direction vector and not a position vector, to solve this use the barrel CFrame lookvector instead to make it go in the same direction.

how do i add the range of the raycasting to be the range of the bullet

LookVector only gives the direction that the barrel is facing, so doing only
local raycastResult = workspace:Raycast(barrel.Position,barrel.CFrame.LookVector * 20,params) won’t work.

Try local raycastResult = workspace:Raycast(barrel.Position, barrel.Position + barrel.CFrame.LookVector * 20, params)

The second parameter of the Raycast function is a direction (not position) vector, so it should just be the direction of the gun:

barrel.CFrame.LookVector * 20

instead of the ray’s end point:

barrel.Position + barrel.CFrame.LookVector * 20
1 Like

Let’s say you set the bullet’s velocity to 100:

vel.Velocity = barrel.CFrame.LookVector * 100

One way to set the range of the bullet would be to find the time it will take the bullet to travel that distance, based on its velocity:

--Based on the formula v = d/t, you can get time by dividing distance over velocity.
local t = (raycastResult.Position - barrel.Position).Magnitude / 100

--One the bullet has traveled long enough, it will be destroyed.
deb:AddItem(newpart, t)

If the bullet moves fast enough, the game lags a bit, etc., the bullet might delete itself at a slightly wrong time (so it would look like the bullet either traveled through the thing it hit or didn’t quite reach it).

For the purpose of having a bullet visual, I might recommend using TweenService. It could look something like this:

--TweenService
local TweenService = game:GetService("TweenService")

--The time we previously calculated for the bullet to travel
local t = (raycastResult.Position - barrel.Position).Magnitude / 100 

--This tween will move the bullet from its starting position (the barrel's CFrame) to its end point (the place it will hit)
local bulletTween = TweenService:Create(newpart, TweenInfo.new(t, Enum.EasingStyle.Linear), {Position = raycastResult.Position})
bulletTween:Play()

--Wait until the tween is completed (that is, the bullet has reached its target)
bulletTween.Completed:Wait()

--Delete the bullet once it has reached the target
newpart:Destroy()

This is just one way to do it. Hope this helps!

2 Likes