In my game, I am trying to check and see if a laser is touching a mirror.
I have read online that people have come across this error before, and in fact, it doesn’t work.
I have seen a few answers, but some look deprecated.
How can I get around this? Here are some of my scripts:
Move script:
local Part = script.Parent
local move = script.Parent.move
game:GetService("RunService").Heartbeat:Connect(function()
if move.Value then
Part.Size = Vector3.new(Part.Size.X,Part.Size.Y,Part.Size.Z + 1)
Part.CFrame = Part.CFrame * CFrame.new(0,0,-0.5)
print('moving')
end
end)
Here is what I have of the stop scripts:
local laser = script.Parent
laser.Touched:Connect(function(hit)
if hit.Parent.Name == "SmokingMirror" then
print("hi")
else
print(hit.Parent)
end
end)
Not any other than the API documentation. I learned it myself.
You call Workspace:Raycast and provide an origin and direction, but also with RaycastParams. In your case, you can include the mirror inside the filtering mode.
Once completing the cast, you get the position. This can be used to resize the part and position it to your liking.
For this case raycast is your best option, it is cheap, fast and easy to use.
In order to use this method you need 3 parameters, the first one is the origin, a Vector3 that defines the start position of the laser; the second one is the direction of the laser (in the example code I will use the mouse hit position as the end position of the beam to calculate the location); and the third parameter is the RaycastParams used to fikter and configure certain aspects of your ray.
-- ...
local origin = Character.HumanoidRootPart.Position -- the origin position for the laser
local direction = (Mouse.Hit.Position - origin).Unit * 25 -- the distance of the ray will be 25 studs.
local rayparams = RaycastParams.new()
rayparms.FilterType = Enum.RaycastFilterType[...]
-- other configurations
local result = workspace:Raycast(origin, direction, rayparams)
-- ...
There might be issues with this code, am on my mobile phone, but it should work fine as reference