Visual raycast tracer issues when hitting walls

I don’t know why my bullets are doing this, when I fire my weapon without hitting anything it goes to the max range like it’s supposed to, when it hits something, it should be creating a visual tracer with the distance of the magnitude, but instead causes the bullets to do this.

I’ve tried other methods on this forum, and it end up either doing the same thing, or having my bullet do weird stuff like going behind my character.

Here is the function for the visual ray.

local function CreateBeam(result, Origin, Direction)
	local midpoint = Origin + Direction / 2
	local LaserPart ="Part")
	LaserPart.Material = Enum.Material.Neon
	LaserPart.BrickColor ="New Yeller")
	LaserPart.CanQuery = true
	LaserPart.Parent = BulletsFolder
	LaserPart.CastShadow = false
	LaserPart.Anchored = true
	LaserPart.CanCollide = false
	local magnitude = (Origin - (Origin + Direction)).Magnitude
	if result then
		magnitude = (Origin - result.Position).Magnitude
	LaserPart.CFrame =, Origin) print(magnitude)
	LaserPart.Size =, 0.2, magnitude)

	Debris:AddItem(LaserPart, 0.02) end

I have no idea how to make it properly trace a ray with the distance of where the bullet hits, does anybody know a solution?

I believe this is causing the issue, the bullet is always positioned at the midpoint.

It should be in the origin then translated in the look directiom divided by 2.

Like this trusted solution.

Using the midpoint should work fine, but the problem is the midpoint in your code is always the midpoint of the maximum length ray. If you hit something, you need to use the midpoint of the shortened ray.

Modify the code like so:

    if result then
		magnitude = (Origin - result.Position).Magnitude
        -- Direction.Unit has a length of 1, multiply by magnitude, divide by two.
        midpoint = Origin + Direction.Unit*magnitude/2

Thank you! works flawlessly, completely forgot to change the midpoint.

1 Like

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