Raycasting not working

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’ve been making an aerial pathfinding system (A* algorithm), and I used raycasting for collision detection between nodes, which seems to be causing an issue

  2. What is the issue? Include screenshots / videos if possible!
    When detecting whether a neighbour is avaliable, meaning you can access it from the neighbouring node without running into a collisionbox, the ray just ignores the wall that should stop the pathfinding from leaving the box

External Media

The green parts are the origin of the raycast, red parts are the destination of the raycast, it should clearly run into a part but it doesn’t?

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried messing around with raycastparams, I tried using both raydirection and raydestination, and I tried out some things I found here but nothing worked out

Here’s the part of the code that’s causing the issue

--for visuals
	local visual1 = Instance.new("Part", workspace)
	visual1.Anchored = true
	visual1.CanCollide = false
	visual1.Size = Vector3.new(1, 1, 1)
	visual1.Color = Color3.new(0.015, 1, 0)
	visual1.Position = node.Position
	local visual2 = Instance.new("Part", workspace)
	visual2.Anchored = true
	visual2.CanCollide = false
	visual2.Size = Vector3.new(.5, .5, .5)
	visual2.Color = Color3.new(1, 0.25, 0.1)
	visual2.Position = node2.Position
	
	--making it so that raycast ignores the visuals (yes the walls are cancollide true that is not the problem)
	--node is the origin, node2 is the destination
	local params = RaycastParams.new()
	params.RespectCanCollide = true
	wait(.1)
	local result = workspace:Raycast(node.Position, node2.Position - node.Position, params)
	print(result)
	--it returns result as nil even though it shouldn't
1 Like

are the nodes can collide off? I see you got there a .RespectCanCollide = true

yes they are created in the script and are cancollide off, but even if they were cancollide on it would constantly print the result not nil

Can you print the direction before the result? Just to verify it going the right direction.

Desktop 2023.10.30 - 14.12.52.03 , just added print(node2.Position - node.Position) to the code (for some reason the beginning of the recording is buggy but it should be good enough)

So just for a test if you make the visual2 part CanCollide true does it print it?

yes it does
image

i see the top and bottom are exactly the same so i would think the node is possibly not being updated? if you used the visuals instead dose it work?

local result = workspace:Raycast(visual1.Position, visual2.Position - visual1.Position, params)

I tried it and the same exact thing happened, it still just ignores the wall

So this raycast result keeps printing the same part locations for the first visual2’s made?

no, since then I turned off visual2’s cancollide, now it just prints nil

does anything print if you get in front of the raycast? its odd because it findes the parts your making but not the wall and you said the wall for sure is cancollide true so see if it sees you?

Desktop 2023.10.30 - 15.45.08.08 (I even put another wall in front of it, still nothing)

When using Raycast I always get confused… so I better get the ray look vector then I multiply it by the distance I desire, like this:

local node2_Position = Vector3.new(15, 24, 30)
local node1_Position = Vector3.new(80, 20, 10)

local Distance = 50 -- distance of ray

local lookVector = (node2_Position - node1_Position).unit

local rayOrigin = node2_Position
local rayDirection = lookVector * Distance

local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

I used your code in studio and it seems to work just fine i get a result anytime i put a part between the two “nodes”.

Would you be able to run this code in another new game or share the whole code to see if we can replicate the issue?

Okay, so I tried this and another issue started happening, but now with the algorithm instead. It keeps checking the same positions over and over again even though they are removed from the possible nodes, so I might just close this because I’m wayyy too confused at this point

Okay at this point I don’t know what’s happening the * 5 solution worked for a while, now it doesn’t, I’m just gonna give up

Okay, so Raycasting does not work, but Blockcasting does? It’s kind of a solution but it has its side effects

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