Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I edit this script to only shoot the ray in the X and Z axis?

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.

4 comments

0

I'm not really sure exactly what your asking. Are you asking so that the camera only moves in four directions?

0

No. I'm not asking for anything related to the camera.

0

I see what you're trying to do... I think that when you're casting the ray, have the Y already set? (If that's even possible?)

0

So you just want a straight ray, correct?

1 answer

✓ Accepted answer

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)

2 comments

0

Thanks! This is exactly what I was talking about.

0

Glad I could help ;)

This discussion is preserved as it was. New answers, comments, and votes are closed.