I've set up a raycasting system, and everything works fine except the part moves past the starting position.

Pretty much ----PartA-----------------------PartB

Here's the code

function Fire(Gun,Unit)
	local Part=Instance.new("Part")
	local Distance=(Gun.Position-Unit.Recolor.Position).magnitude
	local Ray=Ray.new(Gun.Position,(Unit.Recolor.Position-Gun.Position).unit*300)
	local Hit,Position=workspace:FindPartOnRay(Ray,script.Parent)
	Part.Name="Raycast_"..math.random(1,2000000000)
	Part.Anchored=true
	Part.CanCollide=false
	Part.TopSurface="Smooth"
	Part.BottomSurface="Smooth"
	Part.FormFactor="Custom"
	Part.Size=Vector3.new(.2,.2,Distance)
	Part.CFrame=CFrame.new(Position,Gun.CFrame.p)*CFrame.new(0,0,-Distance/2)
	Part.BrickColor=Team.TeamColor
	local Mesh=Instance.new("BlockMesh",Part)
	Mesh.Scale=Vector3.new(.5,.5,1)
	Part.Parent=workspace
	coroutine.wrap(function()
		wait(.5)
		Part:destroy()
	end)()
end

Here's a picture

2 comments

0

I'm going to assume "Unit" is the target?

0

Yes