Ray part in a weird position?

Self explanatory.
It’s basically in a weird position and my theory is it has something to do with the CFrame, not too sure tho.

Script:

local rayPart = Instance.new("Part",script.Parent)
		rayPart.Anchored = true
		rayPart.CanCollide = false
		rayPart.BrickColor = BrickColor.new("New Yeller")
		rayPart.Material = Enum.Material.Neon
		rayPart.Size = Vector3.new(0.2,0.2,(m4Attachment.WorldPosition - target.Position).magnitude)
		rayPart.CFrame = m4Attachment.WorldCFrame - (m4Attachment.WorldPosition - target.Position)/2

GIF:
https://gyazo.com/91e526212d510c310dee3a6a612d00aa

Your CFrame might be off. I recommend using a LookAt function so you can rotate the CFrame towards the end location which is target.Position.

CFrame.new(m4Attachment.WorldPosition, target.Position) * CFrame.new(0, 0, (m4Attachment.WorldPosition - target.Position).Magnitude/2 )

This may work, CFrame.new(pos, lookAt) is sort of meant to be deprecated so you could use a LookAt function alternatively for a undeprecated alternative but currently still works.

Doesn’t exactly work…

https://gyazo.com/3b307d23a21d5fafa65d33ecfd579db2

Is there a discrepancy with m4Attachment.WorldPosition? Create a part where its position is set to that and see if it’s correctly where it should be.

You can test this as well with target.Position to make sure it’s correct.

Here is the formula for the CFrame:
CFrame.new(from, to) * CFrame.new(0, 0, -mag/2)

I’ve tested and it is correct so there shouldn’t be any weird placement issues.

It’s the same thing with replacing the attachment with a part.

https://gyazo.com/09445230ac27689d9bb450c8273e3ba1

Is it the soldier guy shooting the yellow ray or the mobile turret with the red ray? The mobile turret seems fine so I might have to assume it’s the yellow ray one.

Try this one:
CFrame.new(m4Attachment.WorldPosition, target.Position) * CFrame.new(0, 0, -(m4Attachment.WorldPosition - target.Position).Magnitude/2 )

Thank you very much! It works now!

Oh and, yes it was the soldier guy.