How do I fix this raycast?

i’ve learned raycasting recently and i’d like to use my newfounded skills to detect when a car is midair, this is my code so far:

	local pos = PointToCast.WorldPosition - Vector3.new(0,3,0)
	local result = workspace:Raycast(PointToCast.WorldPosition, pos)

	if result then
		if (PointToCast.WorldPosition - result.Position).Magnitude >= 3 then
			airborne = true
		end
	end

i immediately assume this wont work (or return a falses positive) since result.Position will probably return the center of the position, not where the raycast actually hit

how do i fix this?

actually… probable make the ray longer, just do: Vector3.yAxis*5.

other than that its fine. raycastResult.Position returns the position of the hit, just like mouse.Hit

just make sure to add an else to set it to false too (both if there’s not a result / its too far.)

I forgot to mention that you can just run your code without the magnitude if you were to use a 3.
result would return nil if nothing was hit within 3 studs, so its ok

And very very important, use RaycastParameters to avoid hitting the car itself!

The pos variable you are using should actually be a direction, not a position.
Replace it with this.
local pos = Vector3.new(0,-3,0)
Now the ray has a direction of -Y(down), and a length of three studs.

1 Like

i made this script when i was really tired and i subtracted the position by 3 studs. i have no idea why.

anyways, i fixed all of it since i’m more awake now, but if the car flips over midair it doesn’t count as airborne, how could I make it so it does?

edit: i added raycastparams like @ElVaranCubico_YT said to do, i cant tell if it actually works though since when i go up ramps it thinks im airborne again…

this is my totally 100% efficient and optimized code:

local function ChangeFov(fov)
	game.TweenService:Create(workspace.CurrentCamera, TweenInfo.new(0.5), {FieldOfView = fov}):Play()
end

local PointToCast = script.Parent.Car.Value.Body.Body.PointToCast
local Descendants = script.Parent.Car.Value.Body:GetDescendants()

while task.wait() do
	local pos = PointToCast.WorldPosition
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Blacklist
	params.FilterDescendantsInstances = {Descendants}
	local result = workspace:Raycast(PointToCast.WorldPosition, pos, params)
	if result then
		if (PointToCast.WorldPosition - result.Position).Magnitude >= 3 then
			ChangeFov(90)
			camShake:ShakeSustain(CameraShaker.Presets.Earthquake)
		else
			if workspace.CurrentCamera.FieldOfView < 90 then
			else
				ChangeFov(70)
				camShake:StopSustained(0.3)
			end
		end
	end
end

First! the second value on wokrspace:Raycast() is not right. I am extremely sorry for my mistake. this is what you should do:

Currently you are setting the 2 argument to something unrelated thus making it not function as intended. ← as @JarodOfOrbiter said, it’s like the orientation, not the targetPosition!

Second - FilterDescendantsInstances does not filter those instances you’ve added. (using getdescendants) What it does is: filters the descendants of those instances. So you only have to add {script.Parent.Car.Value.Body}. Also you were adding a table inside a table so that wouldn’t work either.

Third - This code is fine

But you can also check it this way

			if workspace.CurrentCamera.FieldOfView >= 90 then
				ChangeFov(70)
				camShake:StopSustained(0.3)
			end
1 Like