Raycasting not working. Also, how to visualize raycast as a part?

So I’m messing with raycasting, trying to understand it for our car AI we are trying to develop. I tried following along with the roblox tutorials and what not, but I ran into an issue.

My goal with this is to make it detect an object in front of it, and if it does detect something there, then it will start moving forward with the throttle.

Whenever I run the game in Roblox Studio, it says, " Part Name: Baseplate Detected", and it starts moving. This is what it’s meant to do, however I’m not sure why it’s detecting the baseplate below it, when it should be raycasting on the Y axis. I also did a quick test, and I dropped the car (with the raycasting script in it) from way up high, and it never detected the baseplate or any object, nor moved. It’s quite likely that I missed something vital or simply did it wrongly. Here’s the script, and an explorer screenshot:

Script:

local start = script.Parent.Parent.Parent.RayModel.Start
local finish = script.Parent.Parent.Parent.RayModel.End
local carmodel = script.Parent.Parent
local car = carmodel.MainCar.Part

local rayOrigin = car.Position
local rayDestination = finish.Position
--local rayDirection = rayDestination - rayOrigin
local rayDirection = Vector3.new(0, -100, 0)
local ray = Ray.new(rayOrigin, rayDirection)

local vehicleseat = carmodel.MainCar.Part.VehicleSeat
local throttle = vehicleseat.Throttle
local part = game.Workspace:FindPartOnRayWithIgnoreList(ray, {car})

function DriveForward()
if part then
		print("Part Name: " .. part.Name .. " Detected")
	vehicleseat.Throttle = 20
	end
end

DriveForward()

Screenshot:

image

Any help is appreciated.

If might go through the baseplate when ray casting towards the Destination. Try adding the Baseplate into the blacklist.

Thanks for the suggestion. I changed it to include the baseplate in the ignore list, and now it doesn’t detect anything and doesn’t move. I feel like there’s something I’m missing - I’m new to raycasting and just started trying to learn it a couple days ago.

Here’s what I changed the ray ignore section of code to:

local baseplate = game.Workspace.Baseplate
local part = game.Workspace:FindPartOnRayWithIgnoreList(ray, {car, baseplate})

If you want to detect something in front of the car, you don’t want to be raycasting downwards (local rayDirection = Vector3.new(0, -100, 0)). Instead you would want to raycast in front of the car, which would require you to use CFrames to raycast relative to where the car is facing.

I see that you know direction = destination - origin, so I’ll just explain how to get the destination vector. You would do something like this:

local destination = (car.CFrame * CFrame.new(0, 0, -1000)).p

CFrames have a LookAt vector, which is necessary to determine where exactly is in front of the car. Assuming the front face of the car part is facing forwards, you would multiply it by a vector in the negative Z direction. You may need to change the offset if some other face should face forward. Then get the positional vector of that combined CFrame by getting it’s p.

To address this, the reason why the baseplate wasn’t detected at a high height is because the raycast only goes as far as the directional vector specifies. Your vector of 0,-100,0 would only raycast downwards 100 studs. In the line of code I gave up, it will only raycast 1000 studs, which should be more than enough to find an object in front of the car. In fact, you can use this to your advantage by setting the raycast distance to a minimum value that you want the ai car to react to that obstacle.

1 Like