Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Moryo7 15
10 years ago

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.

0
I'm not really sure exactly what your asking. Are you asking so that the camera only moves in four directions? soaprocks2 75 — 10y
0
No. I'm not asking for anything related to the camera. Moryo7 15 — 10y
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?) OniiCh_n 410 — 10y
0
So you just want a straight ray, correct? Aethex 256 — 10y

1 answer

Log in to vote
1
Answered by
duckwit 1404 Moderation Voter
10 years ago

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)
0
Thanks! This is exactly what I was talking about. Moryo7 15 — 10y
0
Glad I could help ;) duckwit 1404 — 10y
Ad

Answer this question