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
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.
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)
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.