Bullets not coming from barrel

im using fastcast to get the bullets moving and the real projectile is on the server doing all the hit detection and i fire to all the clients with the same origin and direction to actually render the bullet

but the bullet doesnt come from the barrel like it should instead it starts with its center in the barrel

video:

update code:

function RayUpdated(cast, origin, direction, length, velocity, bullet)
	if bullet == nil then return end

	local bulletLength = bullet.Size.Z
	bullet.CFrame = CFrame.lookAt(origin, origin + direction) * CFrame.new(0, 0, -(length - bulletLength))
end

the bullet is a 0.1 by 0.1, by 88.7 sphere mesh

i want it to look like this
image
(this image uses different code, anything closer than 88.7 studs wont show the bullet so i just use a raycast for it)

the bullet should start from the barrel and move out until it reaches 88.7 studs long then it should stop touching the barrel

is there any way to do this

1 Like

update:
i got it to sometimes come from the barrel but not always

local bulletLength = bullet.Size.Z / 2

you want bulletlength to be bullet.Size.Z because when you set the position it will set the middle of the bullet

yeah i know it will position it from the middle but im trying to position it from the back to the direction not the middle

try this

local pos = origin+direction*bulletLength/2
bullet.CFrame =  = CFrame.new(pos, pos+direction)
1 Like

thank you! i spent days trying to figure that out

also real quick

im trying to fix the ricochet and this is the error message (linked with debug on)

do you know how to fix it?
the server saids i should ricochet but fastcast is breaking

code:

function CanRayPierce(cast, result, velocity)
	local material = string.split(tostring(result.Material), ".")[3]
	local percent = ricochet[material]
	local id = cast.UserData.BulletID
	print(allowedRicochet[id])
	
	if percent then
		local chance = random:NextInteger(1, 100)
		if chance <= percent or allowedRicochet[id] ~= nil then
			if allowedRicochet[id] ~= nil then
				allowedRicochet[id] = nil
			end
			return true
		end
	end
	
	return false
end

function RayPierced(cast, result, velocity, bullet)
	local owner = cast.UserData.Owner
	local id = cast.UserData.BulletID
	local settings = cast.UserData.Properties
	local blackList = cast.UserData.BlackList
	if settings.explosive then return end
	
	local position = result.Position
	local normal = result.Normal
	
	bullet.Size = Vector3.new(bullet.Size.X, bullet.Size.Y, bullet.Size.Z / 2)
	velocity /= 2
	local newNormal = Reflect(normal, velocity.Unit)
	
	cast:SetVelocity(newNormal * velocity.Magnitude)
	cast:SetPosition(position)
end

allowedricochet[id] will be true if the server said i should ricochet (this is for when the bullet is very close to the wall when fired and it wont show so i had to use a different method to show it and the ricochet is different

1 Like

what is your ricochet suppose to do? is the red ball the ricochet direction?

i forgot to put the code in the message its there now the reflect function will just make the bulelt bounce off the wall

local function Reflect(surfaceNormal, bulletNormal)
	return bulletNormal - (2 * bulletNormal:Dot(surfaceNormal) * surfaceNormal)
end

so because of the new length update you gave me the allowedricochet[id] code is gone so its just the normal caster:Fire() but it still breaks

are you sure the ray is not hitting the bullet itself?
image

its not meant to hit it

image
the bullet is parented to workspace.Bullets
i send the params over when i fire to all the clients so it should stay the name

edit: after printing if workspace.Bullets is there it wasnt so i added it back in on the client but it still says its hitting the bullet

so idk why, idk how but i turned off CanQuery on the bullet and it fixed it

thanks for all the help

so… i changed a lot with the piercing function of fastcast and now this doesnt work anymore

sometimes it will come from the barrel and sometimes it wont
i did move the bullet rendering to the server due to the new piercing functions so that may be why

fire code:

local cast = caster:Fire(
	origin,
	direction * 5000,
	shellType.speed,
	behavior
)
cast.UserData.Origin = origin
cast.UserData.Direction = direction
cast.UserData.Owner = plr
cast.UserData.BulletID = id
cast.UserData.Properties = settings
cast.UserData.BlackList = customList
cast.UserData.ShellType = shellType

update code:

function RayUpdated(cast, origin, direction, length, velocity, bullet)
	if bullet == nil then return end

	local bulletLength = bullet.Size.Z
	local pos = origin + direction * bulletLength / 2
	bullet.CFrame = CFrame.new(pos, pos + direction)
end

video:

edit: i changed the update code to:

function RayUpdated(cast, origin, direction, length, velocity, bullet)
	if bullet == nil then return end

	local bulletLength = bullet.Size.Z
	local pos = origin + direction * -(length - (bulletLength / 2))
	bullet.CFrame = CFrame.new(pos, pos + direction)
end

now when it isnt coming from the barrel its just slightly off so ill deal with it for now but if anyone has a way for it to always be coming from the barrel tell me please