#ViewportToRay()
1 messages · Page 1 of 1 (latest)
FindPartOnRay is deprecated
This is how you can raycast correctly:
The example code shows you step by step how to do it
everything else about your code looks fine
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
Well can you show the code for when you used proper raycasting?
try :ScreenPointToRay() instead of :ViewportToRay()
also the origin should be from the muzzle of the gun or the camera (depending on your setup) and not ray.Origin I believe
doesnt that mean i have to change the direction too?
** You are now Level 7! **
i'll try screenpoint for now however
Try screenpoint first yes
same result
Can you send the code in the screenshot
thats the only part of the code that has raycasting
unless do you want to see the beam code?
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
Just the raycast code
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
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
this is the raycast for the railgun
not sure what else you're asking for
If you could send it in text rather than image
So i dont have to line by line rewrite it
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
thanks, ill be back in a moment
alr
Well I tried your code and it works fine for me
This is the current code you are using right?
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
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
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)
np! Just reminder that it starts raycasting from the camera (which means if your game is third person, you can shoot through some walls)
its ok the railgun is meant to shoot through walls