I was wondering if I would be able to convert my old Raycasting system to the new Raycast functions I used for shooting a basketball.
Here is the code.
local Camera = workspace.CurrentCamera
local Shoot = {}
local function GetWorldPosition(POS)
local unitray = Camera:ScreenPointToRay(POS.X, POS.Y, 1)
local worldray = Ray.new(unitray.Origin, unitray.Direction * 500)
local _, worldpos = workspace:FindPartOnRayWithIgnoreList(worldray, {workspace})
--[ Return WorldPos ]--
return worldpos
end
function Shoot:Shoot(Position, PowerValue, ShootEvent, String) --I'm given a Position where the player taps the screen or clicks on the screen
local NewPosition = GetWorldPosition(Position)
if String == "XBOX" then
ShootEvent:FireServer(Position, PowerValue)
else
ShootEvent:FireServer(NewPosition, PowerValue)
end
end
function Shoot:Mobile(Position, PowerValue, ShootEvent)
local NewPosition = GetWorldPosition(Position)
ShootEvent:FireServer(NewPosition, PowerValue)
end
return Shoot
I was basically wondering how I’m able to convert it.
I also had a question about comparing two rays, like some type of predictive pathing where if the player were to shoot the basketball and it landed exactly into the net, how would I go about doing that, as well as how can I figure that out in a code? Anyways, that’s just an option, if anyone can convert my script, I’ll test it and I’ll let you know if it works the same.
local Camera = workspace.CurrentCamera
local Shoot = {}
local function GetWorldPosition(POS)
local unitray = Camera:ScreenPointToRay(POS.X, POS.Y, 1)
local params = RaycastParams.new()
params.FilterDescendantsInstances = {workspace}
params.FilterType = Enum.RaycastFilterType.Blacklist
local worldray = workspace:Raycast(unitray.Origin, unitray.Direction * 500)
--[ Return WorldPos ]--
if worldray then
return worldray.Position
else
return nil
end
end
function Shoot:Shoot(Position, PowerValue, ShootEvent, String) --I'm given a Position where the player taps the screen or clicks on the screen
local NewPosition = GetWorldPosition(Position)
if String == "XBOX" then
ShootEvent:FireServer(Position, PowerValue)
else
ShootEvent:FireServer(NewPosition, PowerValue)
end
end
function Shoot:Mobile(Position, PowerValue, ShootEvent)
local NewPosition = GetWorldPosition(Position)
ShootEvent:FireServer(NewPosition, PowerValue)
end
return Shoot
it seems you don’t need raycasting as you are ignoring all the workspace descendants. In the documentation it says: If the ray does not intersect anything, the return values will be nil and the point at the end of the ray, respectively.
so do this
local function GetWorldPosition(POS)
local unitray = Camera:ScreenPointToRay(POS.X, POS.Y, 1)
local worldray = Ray.new(unitray.Origin, unitray.Direction * 500)
local _, worldpos = workspace:FindPartOnRayWithIgnoreList(worldray, {workspace})
--[ Return WorldPos ]--
return worldpos
end
is the same as doing this
local function GetWorldPosition(POS)
local unitray = Camera:ScreenPointToRay(POS.X, POS.Y, 1)
return unitray.Origin + unitray.Direction * 500
end
simply by comparing their main characteristics, that is, by comparing their points of origin (comparison between vectors), direction (comparison between unit vectors), or distance (numerical comparison). The problem is that since all these numbers are floating-point numbers you would have to make an approximate comparison.
For example, instead of doing this
number1 == number2
you would do this
local tolerance = 0.05
number1 >= number2 - tolerance and number1 <= number2 + tolerance
I didn’t understand this. Could you explain a little more?