Help with raycasts needed

Im rewriting some of my code for my gun system and i need to figure out the “impact vector” so the shell can penentrate a part (the white line).

In my old system i just had a point to which the gun fires too really far away but I want to shorten this distance so i need the direction of the ray as it hits the part (the gun has bulletdrop)
I am using the FastCast module

relevant code:

function GunModule.HitPenetrate(result,RayVector)
	--GunModule.Fragmentation(result,RayVector)
	--GunModule.AmmoCookOff(result,true)
	
	local Origin = result.Position + (RayVector - result.Position).Unit * ArmorThickness -- RayVector == FiredGun.MaxRange.WorldPosition
	local Direction = (RayVector - Origin).Unit
	Caster:Fire(Origin,Direction,TankRoundInfo[SelectedAmmo].Speed,CastBehavior) -- the ray that comes out of the other side of the hit part
end

function GunModule.Hit(cast, result, speed, bullet)
	print(cast)
	coroutine.resume(coroutine.create(function()
		if result.Instance:IsA("BasePart") then
			if result.Instance.Anchored == false then
				result.Instance.Anchored = true
				task.wait()
				result.Instance.Anchored = false
			end
		end
	end))
	
	if result.Instance:FindFirstAncestorWhichIsA("Model") then
		if result.Instance:FindFirstChild("Humanoid") then
			result.Instance.Humanoid:TakeDamage(math.huge)
		end
	end
	
	if Tank then
		local s,f = pcall(function()
			local PlrWhoShot = FiredGun.Parent.Parent.Parent.Parent:FindFirstChild("TurretSeat").Occupant.Parent.Name or FiredGun.Parent.Parent.Parent.Parent.Parent:FindFirstChild("TurretSeat").Occupant.Parent.Name
			Tank.Parent.LastHit.Value = game.Players[PlrWhoShot]
		end)
	end
	
	local normal = cast:GetVelocity().Unit
	local ImpactVector = normal - (2 * normal:Dot(result.Normal) * result.Normal)
	local ImpactAngle = math.round(math.deg(math.asin(result.Normal:Dot(ImpactVector))))
	if ImpactAngle == "nan" then
		ImpactAngle = 0
	end
	
	local CastParams2 = RaycastParams.new()
	CastParams2.FilterDescendantsInstances = {result.Instance}
	CastParams2.FilterType = Enum.RaycastFilterType.Include
	local NewOrign = result.Position + (FiredGun.MaxRange.WorldPosition - result.Position).Unit * 4
	local Direction = result.Position-NewOrign
	local RayCast = workspace:Raycast(NewOrign, Direction, CastParams2)
	
	DebugPrint("Impact angle: "..ImpactAngle)
	CreateHole(result)
	
	if ImpactAngle > TankRoundInfo[SelectedAmmo].BounceAngle then -- Doesnt bounce
		if RayCast then
			ArmorThickness = GetArmorThickness(RayCast,result)

			if ArmorThickness < CanPen then 							-- Can pen the armor
				DebugPrint("Round can pen", ArmorThickness)
				GunModule.HitPenetrate(result,FiredGun.MaxRange.WorldPosition)
				CanPen -= ArmorThickness
			else														-- Cant pen the armor
				DebugPrint("Round can not pen", ArmorThickness)
				if TankRoundInfo[SelectedAmmo].Explosive == true then
					GunModule.Explosion(result)
				end
			end
		end
	else
		GunModule.Bounce(result,ImpactVector)
	end
end

Any help is welcome!

1 Like

Do you mean something like this?

Looks like I lost the dir variable somewhere but you can probably figure that out

1 Like

no, not really, i just need the dir of the ray which hits the part

1 Like

The direction in this case is going to be the velocity of the part. The velocity means that it is going at the speed of the magnitude of the velocity, with the direction of the unit vector of the velocity.

1 Like

But the part isnt moving or do just understand it wrong?

1 Like

With fastcast one of the variables that is passed over in the RayHit event is a Vector3 that represents the simulated velocity at that point.


Using the Unit vector of that velocity is going to give the direction that the bullet was travelling when the ray hit something.

1 Like

ah ok, but i must have done smth wrong, cause the ray shoots out to the side


I replaced the FiredGun.MaxRange.WorldPosition with speed.Unit
Edit, it will always go to the same position
grafik

1 Like

I think it has to do with this line of code:

since you are now passing in the velocity vector, just use that instead of calculating a direction.

1 Like

You can also try implementing the built-in function called CanPierceCallback that fastcast has. That way, the module will handle the piercing for you.

Edit: sorry, it is actually called CanPierceFunction, and it is a part of the FastCastBehavor that is sent into the cast.

Ok, this does kinda fix it but the frag (i removed that to make the main ray more visible) strear off to the side, i also noticed that the ray that comes the other side of the part is also shifted to the right a bit (marked it red)

Edit heres the code for the frag

function GunModule.Fragmentation(result,RayVector)
	local FragCastBehavior = FastCast.newBehavior()
	local FragCaster = FastCast.new()
	local FragOrigin = result.Position + (RayVector - result.Position).Unit * ArmorThickness
	FragCastBehavior.Acceleration = Vector3.new(0,-(TankRoundInfo[SelectedAmmo].Speed),0)
	FragCastBehavior.CosmeticBulletContainer = workspace.ActiveTankRounds
	FragCastBehavior.CosmeticBulletTemplate = Frag
	FragCastBehavior.MaxDistance = SecondRayMaxDistance
	
	for i=TankRoundInfo[SelectedAmmo].FragAmount*ArmorThickness,0,-1 do
		local MaxDeviate = math.rad(TankRoundInfo[SelectedAmmo].Spread)
		local RandGen = Random.new()
		local FrageCFrame = CFrame.lookAt(result.Position, FragOrigin) * CFrame.Angles(0, 0, RandGen:NextNumber(0, math.pi * 2)) * CFrame.Angles(RandGen:NextNumber(0, MaxDeviate), 0, 0)
		local NewDirection = FrageCFrame.LookVector
		
		FragCaster:Fire(FragOrigin,NewDirection*SecondRayMaxDistance,TankRoundInfo[SelectedAmmo].Speed,FragCastBehavior)
		
		FragCaster.RayHit:Connect(function(cast, result, speed, bullet)
			GunModule.AmmoCookOff(result)
			if result.Instance:FindFirstAncestorWhichIsA("Model") then
				if result.Instance:FindFirstChild("Humanoid") then
					result.Instance.Humanoid:TakeDamage(CharFrageDamage)
				end
			end
			
			--print(bullet)
			--TODO delete the bullet
		end)
		
	end
end

Alright, your code to find the position of the next ray origin is off by a little, and that is why it is going off to the side. It should look more like this:

local Origin = result.Position + RayVector * ArmorThickness

This may also be affecting your frag code because you are finding the origin the same way.

1 Like

Yep, after pasting this, it fixed it, had to paste it into the frag code too but it works now


Thx for the help

1 Like

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