I need help with a gun with FastCast

I’m trying to make some sort of over-the-shoulder type of game, so my gun casts a ray from the middle of the screen so the bullet can go towards the raycast’s result’s position.

The problem is that when using FastCast (Click here if you don’t know what FastCast is) and using the same method is that it doesn’t actually go towards the result’s position as shown in this recording:

I believe what is wrong is because the second parameter in the Fire function is the direction and not look at (like, look at from the Cframe.new()), but, I don’t really know how to make it so the direction is the “look at” of the raycast result. Any help is appricated! :slight_smile:

local function OnFire(plr, mouseHit, tool)
	script.Parent.Fire:Play()
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {tool, tool.Handle.Shoot, plr.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.IgnoreWater = true
	
	local behavior = fastcast.newBehavior()
	behavior.RaycastParams = raycastParams
	behavior.AutoIgnoreContainer = true
	behavior.CosmeticBulletTemplate = RS.TypesOfBoolets.AdminBullet
	behavior.CosmeticBulletContainer = workspace.BooletZone
	
	caster:Fire(tool.Handle.Shoot.Position, mouseHit, 10, behavior)
end

The second parameter in the Fire function expects a direction vector, not a target position.

You need to calculate the direction vector from the gun’s muzzle to the hit position and use this direction vector your function.

local function OnFire(plr, mouseHit, tool)
    script.Parent.Fire:Play()
    
    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {tool, tool.Handle.Shoot, plr.Character}
    raycastParams.FilterType = Enum.RaycastFilterType.Exclude
    raycastParams.IgnoreWater = true
    
    local behavior = fastcast.newBehavior()
    behavior.RaycastParams = raycastParams
    behavior.AutoIgnoreContainer = true
    behavior.CosmeticBulletTemplate = game.ReplicatedStorage.TypesOfBoolets.AdminBullet
    behavior.CosmeticBulletContainer = workspace.BooletZone
    
    -- Calculate the direction vector
    local shootPosition = tool.Handle.Shoot.Position
    local direction = (mouseHit.Position - shootPosition).Unit
    
    -- Fire the bullet
    caster:Fire(shootPosition, direction, 10, behavior)
end

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.