Align bullet rotation to raycast direction

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?

CFrame.new(origin:Lerp(intersection,0.5),intersection)

replaces

raycastLine.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
1 Like

Thanks for teh solution, but it’s for the white part / bullet, not the red / orange line.
ppreciate the help

ohhh sorry haha i didnt see it but according to it logic seems fine?

1 Like

Not really, by replcaing the raycastLine.CFrame to your code, the raycastLine (the red line) now spawn further from the origin position

so that is the solution? or not?

No, sadly. I want the visualBullet to rotate as the raycastLine rotation


I tried using :

visualBullet.Rotation = raycastLine.Rotation

But to no avail

wait why are you even using those visual bullets? are they just like for the animation? also my idea is you use lerp, lerp from first point to the second

1 Like

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