Raycast is stopping when touching air

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want my raycast to behave normally
  2. What is the issue? Include screenshots / videos if possible!
    It is giving me this bug: nil 1227.9871826171875, -3.4028234663852886e+38, 851.6358642578125 0, 0, 0 Enum.Material.Air
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    No one has had this problem, suprisingly
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local UserInputService = game:GetService("UserInputService")
local RP = game:GetService("ReplicatedStorage")
local canClick = true
local Raycastparams = RaycastParams.new()
Raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
Raycastparams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
local Player =game.Players.LocalPlayer.Character

function Aim()
	local Player =game.Players.LocalPlayer.Character
	local cursorPosition = game:GetService("UserInputService"):GetMouseLocation()
	local oray = game.workspace.CurrentCamera:ViewportPointToRay(cursorPosition.x, cursorPosition.y, 0)
	--local ray = Ray.new(game.Workspace.CurrentCamera.CFrame.p,(oray.Direction * 1000))
	local ray = workspace:Raycast(Player.HumanoidRootPart.Position, oray.Direction * 1000)
	print(ray.Instance)
	return ray.Instance
end

while true do
	UserInputService.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 and canClick == true then
			canClick = false	
			local part = Aim()
			if part ~= nil and part.Parent ~= nil and part.Parent.Parent ~= nil and  part.Parent ~= Player and Player:FindFirstChild("ScopedRifle") then
				if part.Parent:FindFirstChild("Humanoid") then
					RP:FindFirstChild("keyPressed"):InvokeServer("Gun",part.Parent)
				elseif  part.Parent.Parent:FindFirstChild("Humanoid") and part.Parent.Parent.Parent ~= Player then	
					RP:FindFirstChild("keyPressed"):InvokeServer("Gun",part.Parent.Parent)
				end
			end
			wait(0.1)
			canClick = true
		end
	end)
task.wait(0.1)	
end

I would also like the ray to ignore the player’s entire character

Isn’t a bug it just means it hasn’t hit anything for 1,000 studs in that direction. You need to check if the Instance property is actually a thing

So, instead of checking if its nil I check if it is equal to… what?
Enum.Air or something?

You could try this:

local Player = game.Players.LocalPlayer.Character
local cursorPosition = game:GetService("UserInputService"):GetMouseLocation()
local oray = game.workspace.CurrentCamera:ViewportPointToRay(cursorPosition.x, cursorPosition.y, 0)

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsType = {Player}

local ray = workspace:Raycast(Player.HumanoidRootPart.Position, oray.Direction * 1000, params)

if ray.Instance then
    return ray.Instance
end

I did that just now, but how to I check if it hit nothing? Also, this is happening even if I click on the baseplate. It always thinks its hitting air

The entire ray I believe is equal to nil if it hit nothing

told it to print what it was hitting the return of aim, and it gives this:nil -212.3601531982422, -3.4028234663852886e+38, 1477.05712890625 0, 0, 0 Enum.Material.Air . I am not clicking on anything other than the baseplate.

also, if I climbed up higher, it gave me actual results. All I’m trying to do here is cast a ray from the character, and check if it hit another character.

What about this:

local Player = game.Players.LocalPlayer.Character
local Mouse = game.Players.LocalPlayer:GetMouse()
local cursorPosition = Mouse.Hit.Position
local oray = game.Workspace.CurrentCamera:ViewportPointToRay(cursorPosition.x, cursorPosition.y, 0)

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsType = {Player}

local ray = workspace:Raycast(Player.HumanoidRootPart.Position, oray.Direction * 1000, params)

if ray and ray.Instance then
    return ray.Instance
end

and player is replaced with game.Players.localplayer.character, right?

You can keep everything the same, as it should work.

It seems like its only returning when i click on the player

it fixed it, I think it was because I was defining the params before the player was loaded in

I think something about the direction of the ray is messed up, how about just checking if the camera can see it? How would I do that? I mean, it’s definently casting a ray, but it just does it in the wrong direction

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.