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

Starting a raycast from where a part is facing?

Asked by 8 years ago

In a shooter I'm making, the player is forced into Shift Lock and I made a script so the player's arm (that holds the weapon) and head move up and down with the cursor.

To make my gun fire, I attempted to raycast starting from the "Head" of the gun (the front), but the problem is always that it's never in the angle I want. The X and Z are always perfectly aligned with the gun, but I can never get the ray to have the same angle as the gun's head.

This is what I've been trying:

local tlv = script.Parent.Parent.Torso.CFrame.lookVector
        local hlv = script.Parent.Head.CFrame
        local hit, position, normal = workspace:FindPartOnRay(
            Ray.new(
                script.Parent.Head.CFrame.p,
                Vector3.new(tlv.x * 1000, hlv.p.y, tlv.z * 1000)
            ),
            script.Parent.Parent

        )

        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Bright red")
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0.25
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false
        local distance = (script.Parent.Handle.CFrame.p - position).magnitude
        beam.Size = Vector3.new(0.3, 0.3, distance)
        beam.CFrame = CFrame.new(script.Parent.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

The beam is copied from Roblox's tutorial on making a Laser Gun, and is just so I can see where the ray fires. I need to avoid using LocalScripts for this solution as well.

The beam is always a straight line starting from the head of the gun, and doesn't change its angle with the player.

By somehow starting the raycast at the part, I can avoid this problem.

How would I do this?

0
So you want the ray to be cast straight from the barrel (not taking into account the position of mouse like normal raycast guns)? As in, the bullet will only fire straight from the barrel, to be realistic TheDeadlyPanther 2460 — 8y
0
Yes Kampfkarren 215 — 8y
0
Ok, let me figure this out. You could consider making a part inside the tool (invisible, cancollide false). Then you just weld it and use it to make your ray. TheDeadlyPanther 2460 — 8y
0
Problem is that it would have the same effect, where the ray would be straight and not angled. Kampfkarren 215 — 8y
View all comments (9 more)
0
No, it wouldn't. If you made the part beforehand, and simply made it a part of the tool (welding it to the handle), it would be mean that it would face the way the tool is facing. TheDeadlyPanther 2460 — 8y
0
Are you implying the Head doesn't face the way the tool is facing? It's the front... Kampfkarren 215 — 8y
0
Also, you still didn't explain how I would make the ray from that. Kampfkarren 215 — 8y
0
The invisible part would be welded to the tool, meaning that, if you moved/rotated the tool, it would retain the exact same rotation and offset as the tool. Try welding a part to your head or something, then move your character and see how it reacts. TheDeadlyPanther 2460 — 8y
0
To make the ray would be simple, you just need to get the end and the start of the part. TheDeadlyPanther 2460 — 8y
0
Are you suggesting a long invisible brick and detect Touch or something? If you are, no. Kampfkarren 215 — 8y
0
No, not detecting touch, but a long invisible brick, yes. I don't know any other way to cast the ray. Note that the invisible brick is probably one of the best ways to do it without complex maths. TheDeadlyPanther 2460 — 8y
0
I'd rather do complex maths then a solution like that to be honest. Kampfkarren 215 — 8y
0
Well, good luck finding help then :P TheDeadlyPanther 2460 — 8y

Answer this question