#ViewportToRay()

1 messages · Page 1 of 1 (latest)

rocky garnet
#

im trying to use a viewport to ray to make a laser that shoots from the barrel of a gun to a crosshair at a definite position (not mouse). Im struggling to get the position from the said ray though, i dont really know how i should do this.
i've tried using workspace:Raycast() but it had a bit of an offset for some reason

#

if ur wondering about workspace:FindPart it was FindPartOnRay (which did NOT work)

fallen radish
#

FindPartOnRay is deprecated

#

The example code shows you step by step how to do it

#

everything else about your code looks fine

rocky garnet
#

i did try that

#

however the beam was aiming to an offset

#

i did workspace:raycast(ray.Origin, ray.Direction.Unit*500, params)

#

params being tool and character

fallen radish
#

Well can you show the code for when you used proper raycasting?

rocky garnet
#

@fallen radish

fallen radish
#

also the origin should be from the muzzle of the gun or the camera (depending on your setup) and not ray.Origin I believe

rocky garnet
#

doesnt that mean i have to change the direction too?

rich lavaBOT
#

studio** You are now Level 7! **studio

rocky garnet
#

i'll try screenpoint for now however

fallen radish
#

Try screenpoint first yes

rocky garnet
#

same result

fallen radish
#

Can you send the code in the screenshot

rocky garnet
#

this is test with the barrel as origin

rocky garnet
#

unless do you want to see the beam code?

fallen radish
#

I think the reason is because the poisiton of your crosshair is the top left corner (that is the default) change the crosshairs anchor point to 0.5, 0.5

fallen radish
rocky garnet
#

should i change back the origin to ray.origin?

#

fyi im only trying to get the position of the ray hit, the beam will still come from the barrel despite the ray going from the camera

fallen radish
#

If your game is 3rd person its better to raycast from the same location as the beam and not the camera

#

Send the code for the raycast and ill let you know

rocky garnet
#

not sure what else you're asking for

fallen radish
#

If you could send it in text rather than image

#

So i dont have to line by line rewrite it

rocky garnet
#

ah

#

whats the format again?

#

lua.new?

fallen radish
#
local test = 100
rocky garnet
#
local function Fire()
    local toolpos = tool.Handle.Barrel.Position
    print(fuel)
    if fuel == 100 then
        toolequip:Fire("Railgun",true, true)
        hum.WalkSpeed = 0
        if button then
            local mobcrosspos = mobcross.AbsolutePosition
            local ray = camera:ScreenPointToRay(mobcrosspos.X, mobcrosspos.Y)
            local params = RaycastParams.new()
            params.FilterType = Enum.RaycastFilterType.Exclude
            params.FilterDescendantsInstances = {char}
            local raycast = workspace:Raycast(ray.Origin, ray.Direction.Unit*500, params)
            if raycast and raycast.Instance then
                mpos = raycast.Position
                print(mpos)
                fuel = 0
                toolfire:FireServer(2, mpos, toolpos)
            end

            
        else
            mpos = mouse.Hit.Position
            fuel = 0
            toolfire:FireServer(2, mpos, toolpos)
        end
    end
end
#

there

fallen radish
#

thanks, ill be back in a moment

rocky garnet
#

alr

fallen radish
#

Well I tried your code and it works fine for me

fallen radish
#

I think I know what the issue is tho

#

I have a custom crosshair as well and the absolute position is the top left corner and not the center

#

If make your crosshair not be transparent as well, you should be able to see the same thing happening

#

If you can confirm this to be true I can provide the fix

rocky garnet
#

oooh ok let me test hold on

#

yes

#

that is in fact whats happening

#

anchor point is set to 0.5 tho so i dont know why its doing that

fallen radish
#

AbsolutePosition doesn't respect the anchor point I guess. To fix this just replace:

local mobcrosspos = mobcross.AbsolutePosition 

with

local mobcrosspos = mobcross.AbsolutePosition + (mobcross.AbsoluteSize/2)
rocky garnet
#

huh

#

ok then

#

OK IT WORKS NOW

#

THANK YOU

fallen radish
#

np! Just reminder that it starts raycasting from the camera (which means if your game is third person, you can shoot through some walls)

rocky garnet
#

its ok the railgun is meant to shoot through walls

fervent bluff
#

Yo @rocky garnet

#

I had done something similar

#

I can show you what I have done