I’m standing directly infront of a wall, and ray.Instance is nil.
It doesnt print as all.
I dont really work with raycasting, cframes and stuff so yea
--// VARS
local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerService = game:GetService("Players")
local Player = PlayerService.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
local params = RaycastParams.new()
--// VFX EMMITERS
local Shard = ReplicatedStorage.VFX.Shards
--// FUNCTIONS
UIS.InputBegan:Connect(function(Input, IsTyping)
if Input.KeyCode == Enum.KeyCode.F and not IsTyping then
local LookVector = HRP.CFrame.LookVector
local ray = workspace:Raycast(HRP.Position, Vector3.new(LookVector * 50), params)
if ray and ray.Instance then
ray.Instance.Color = Color3.new(1,0,0)
print(ray.Position)
end
local NewPart = Instance.new("Part")
NewPart.Transparency = 1
NewPart.Anchored = true
NewPart.Size = Vector3.new(1, 1, 1)
NewPart.CanCollide = false
end
end)
I wrote a simple raycasting module, here you go. Even has Input-based casting.
local RaycastModule = {}
-- Constants
local RAYCAST_DISTANCE = 1000
local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {workspace.CurrentCamera}
local function rayCast(origin, direction,passed_Params)
local raycastParam = passed_Params or Params
local worldRay = Ray.new(origin, direction.Unit * RAYCAST_DISTANCE)
local raycastResult = workspace:Raycast(worldRay.Origin, worldRay.Direction * RAYCAST_DISTANCE, raycastParam)
-- Optional beam visualization
if raycastResult then
local size = Vector3.new(0.2, 0.2, (worldRay.Origin - raycastResult.Position).Magnitude-2)
local cframe = CFrame.lookAt(worldRay.Origin, raycastResult.Position) * CFrame.new(0, 0, -(size.Z/2)-2)
else
print("no hit")
end
return raycastResult
end
function RaycastModule.rayCastInDirection(origin, direction,passed_Params)
return rayCast(origin, direction,passed_Params)
end
function RaycastModule.rayCastToPoint(origin, goalPos,passed_Params)
local direction = goalPos - origin
return rayCast(origin, direction,passed_Params)
end
function RaycastModule.rayCastToInput(origin, inputObject,passed_Params)
local Camera = workspace.CurrentCamera
local worldRay = Camera:ScreenPointToRay(inputObject.Position.X, inputObject.Position.Y)
return rayCast(origin, worldRay.Direction,passed_Params)
end
return RaycastModule