Hello.
I’m encountered some issue with my raycast system.
You see, I made a raycast. If the raycast is successful, create a fake bullet from the origin to the final position.
However, the bullet aren’t aligned correctly.
script :
local function doRaycastAndDamage(base, position)
if magazine.Value <= 0 then return end
local maxDis = 750
local maxS = 7.5
local minS = 0
local mouseDirection = (position - base.WorldPosition).Unit
local directionalCF = CFrame.new(Vector3.new(), mouseDirection)
local direction = (directionalCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(minS, maxS)), 0, 0)).LookVector * maxDis
local rayparen = RaycastParams.new()
rayparen.FilterType = Enum.RaycastFilterType.Blacklist
rayparen.FilterDescendantsInstances = {script.Parent, game.Workspace.BUllet, script.Parent.Parent}
rayparen.IgnoreWater = false
local origin = base.WorldPosition
local result = game.Workspace:Raycast(origin, direction, rayparen)
local intersection = result and result.Position or origin + direction
local distance = (origin - intersection).Magnitude
local raycastLine = Instance.new("Part")
raycastLine.Size = Vector3.new(0.05, 0.05, distance)
raycastLine.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
raycastLine.Parent = game.Workspace.BUllet
raycastLine.Anchored = true
raycastLine.CanCollide = false
raycastLine.Material = Enum.Material.Neon
raycastLine.BrickColor = BrickColor.new("Neon orange")
local visualBullet = game.ReplicatedStorage:WaitForChild("Part"):Clone() -- This is the bullet that need to be rotated
visualBullet.Parent = game.Workspace.BUllet
visualBullet.Anchored = true
visualBullet.CanCollide = false
visualBullet.Position = origin
if result ~= nil then
local g = game:GetService("TweenService"):Create(visualBullet, TweenInfo.new(3.5), {Position = result.Position})
g:Play()
g.Completed:Connect(function()
visualBullet:Destroy()
end)
end
game.Debris:AddItem(raycastLine, 0.1)
if script.Parent.BulletBarrel.ParticleEmitter ~= nil then
script.Parent.BulletBarrel.ParticleEmitter:Emit(math.random(1, 2))
end
script.Parent["TurretFiring"]:Play()
if result then
local part = result.Instance
local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
local tag = part.Parent:FindFirstChild("IsAnEnemy")
if humanoid and tag and tag.Value == true then
humanoid:TakeDamage(damage.Value)
end
end
magazine.Value -= 1
end
How does I make it so the fake bullet align with the rotation / aligned with raycastLine?