So I've got this code here:
wait(10) game.Players.LocalPlayer:GetMouse().Button1Down:connect(function() local ray = Ray.new(game.Players.LocalPlayer.Character.Torso.CFrame.p, (game.Players.LocalPlayer:GetMouse().Hit.p - Vector3.new(game.Players.LocalPlayer:GetMouse().Hit.y) - game.Players.LocalPlayer.Character.Torso.CFrame.p).unit*100) local hit, position = game.Workspace:FindPartOnRay(ray, game.Players.LocalPlayer.Character) local didithit = hit if didithit then print(hit.Name) end local distance = (position - game.Players.LocalPlayer.Character.Torso.CFrame.p).magnitude local rayPart = Instance.new("Part", game.Players.LocalPlayer.Character) rayPart.Name = "RayPart" rayPart.BrickColor = BrickColor.new("Bright red") rayPart.Transparency = 0.5 rayPart.Anchored = true rayPart.CanCollide = false rayPart.TopSurface = Enum.SurfaceType.Smooth rayPart.BottomSurface = Enum.SurfaceType.Smooth rayPart.formFactor = Enum.FormFactor.Custom rayPart.Size = Vector3.new(0.2, 0.2, distance) rayPart.CFrame = CFrame.new(position, game.Players.LocalPlayer.Character.Torso.CFrame.p) * CFrame.new(0, 0, -distance/2) game.Debris:AddItem(rayPart, 0.1) end)
It works perfectly fine as is, I made it with help from the Wiki. But I've got an issue with an edit I want to make with it. When the ray is shot, I'd like it to fire the ray using the X and Z axis, but not the Y Axis. This sounds a little vague, so let me explain.
I've got an Isometic camera (Top Down). When the ray fires, it currently will hit the floor, due to the ray firing exactly where my mouse is pointing to. I'd like it to only fire left, right, forward, and backward. Not up or down. I've toyed with it a bit, but any change I make to the scripts end up breaking it.
This is actually quite straightforward: You want the direction of the Ray
to be confined to a plane parallel with the x-z plane but at an elevation of where the Ray
is projected from in the y-axis.
I'm just going to deconstruct and rewrite your Ray
instantiation code, so:
local ray = Ray.new(game.Players.LocalPlayer.Character.Torso.CFrame.p, (game.Players.LocalPlayer:GetMouse().Hit.p - Vector3.new(game.Players.LocalPlayer:GetMouse().Hit.y) - game.Players.LocalPlayer.Character.Torso.CFrame.p).unit*100)
becomes:
--It's much easier, and more efficient, to have stored references to these objects player = game.Players.LocalPlayer character = player.Character torso = character.Torso mouse = player:GetMouse() range = 100 origin = torso.CFrame.p direction = (mouse.Hit.p - torso.CFrame.p) --Now all you need to do is exclude the y-component from the direction direction = Vector3.new(direction.x, 0, direction.z).unit*range local ray = Ray.new(origin,direction)