Issue with Raycast laser

Hi, i have a problem w my raycast laser, its supposed to detect collision and then move the beam attachment to raycastresult position, but this happens

what i want it to do :


make it stay nice and straight right?
but instead, this happens :


(if video doesnt work: https://gyazo.com/c6de920f3debb2b1b93c99789be47725)

im not too experienced with both raycast and cframing, so how could i fix this?

local part = workspace.Caster
local origin = part.Position
local direction = part.CFrame.LookVector*500
local stepped = game:GetService('RunService').Stepped
local at1 = part.Attachment1
local at2 = part.Attachment0

at1.Position = at2.CFrame.LookVector*500

stepped:connect(function()
	local result = workspace:Raycast(origin, direction)

	if result then
		at1.Position = result.Position
	else
		at1.Position = at2.CFrame.LookVector*500
	end
end)

1 Like

Try using WorldPosition instead of Position when setting the position of Attachment 1. The Position property for Attachments is used as an offset to it’s parent’s position.

2 Likes

This seems to be the problem as it is mixing up axis

How could i place the attachment 1 to result position?
would i just do this?

if result then
attachment1.Position = result.WorldPosition
end

No, change the property you’re writing the information too. You’re currently doing at1.Position when for your case you want to be using at1.WorldPosition.

if result then
    at1.WorldPosition = result.Position
end
1 Like

that kinda fixed it for one axis, but this happens now :

how would i tackle this? my first thought was a1.WorldPosition.Y = result.Position.Y
keep in mind, i want it to stay straight 100%
honestly, i got no idea

if result then
    at1.WorldPosition = Vector3.new(result.Position.X, at2.WorldPosition.Y, result.Position.Z)
end

could you explain this, it fixed this, but i dont know how exactly lol

This is unrelated but could you give @xZylter the solution because he did most of the work.

Why it works: Vector3’s have three coordinates, the X, Y and Z. To reference any of the numbers you can just do Position.X. For example to find where a part is on the X axis you can just do:

local X = part.Position.X

The code was working well for the X and Z coordinates, but the Y axis was coordinates. So I basically just kept the X and Z coordinates, but made sure that the Y coordinate was the same as the other attachment’s Y coordinate.

If that still doesn’t make sense let me know and I can explain it further.

my goal was to put this on a weapon, kinda like how phantom forces does their lasers, they react to parts etc, but now i have this happening

the ray and beam completely kinda brick if i move the caster part

It’s probably because you define all of the variables in the top of the script, so those variables won’t change when the part moves. Try this:

local part = workspace.Caster
local stepped = game:GetService('RunService').Stepped
local at1 = part.Attachment1
local at2 = part.Attachment0

at1.Position = at2.CFrame.LookVector*500

stepped:connect(function()
	local result = workspace:Raycast(part.Position, part.CFrame.LookVector*500)

	if result then
        at1.WorldPosition = Vector3.new(result.Position.X, at2.WorldPosition.Y, result.Position.Z)
    end
		at1.Position = at2.CFrame.LookVector*500
	end
end)
1 Like

well damn it works perfectly now, thanks for the help!

1 Like

this is happening only on Y axis because of
at2.WorldPosition = Vector3.new(result.Position.X, at1.WorldPosition.Y, result.Position.Z)
not sure how to fix this, i feel like :ToWorldSpace() is the solution here but not sure how to even use that at all
i tried to remove the at1.WorldPosition.Y line but the laser will work once properly and then the attachments will just spaz out

edit: fixed this by doing result.Position.Y instead of at1.Position.Y

local part = script.Parent
local stepped = game:GetService('RunService').Stepped
local at2 = part.Attachment1
local at1 = part.Attachment0
local at3 = part.LaserHit

at2.Position = at1.CFrame.LookVector*5000

stepped:connect(function()
	local result = workspace:Raycast(part.Position, part.CFrame.LookVector*5000)

	if result then
		at2.WorldPosition = Vector3.new(result.Position.X, at1.WorldPosition.Y, result.Position.Z)
	else
		at2.Position = at1.CFrame.LookVector*5000
    end
end)