How to make a simple obstacle avoidance system?

I really wan to make a obstacle avoidance system but I don’t know how to go about doing it, I don’t mean as in a very high tec one a mean it raycasts from the part and if it finds a wall then it will turn to avoid it.

Image

The blue arrows are raycasts the trouble I am having is how to go about decide if the part should go left or right 1 or -1, I know a little about Dot and Cross vectors and a fear deciding which way to go will involve it.

Can somebody help me out?

My idea is you would use the normal vector of the surface the raycast hits, and then use the orthogonal vector of the normal vector of the surface and the unit up vector to direct the object. It looks something like this:

Since you only want the object to go left or right, you can take the cross product of the unit up vector and the normal vector of the surface. Then, you make the object smoothly transition to the vector d (the direction vector). Repeat this until the object does not find a surface in front of it.

I made something for this awhile ago (though using a full blown pathfinder for something like this might be slightly overkill)

Pathfinding.rbxl (27.1 KB)
it’s incredibly incompetently made and gets comically laggy if you make it calculate too much especially so if the target destination is moved around in runtime, but if you’re just pathfinding your part around static parts in a semi-linear environment it should works fine

1 Like

I understood a bit of that I have got Raycast.Normal:Cross(Vector3.new(0, 1, 0)) but the problem is I am using W A S D to steer meaning

local MovementTable = {0, 0, 0, 0}

local ForwardBackward = MovementTable[1] - MovementTable[3]
local LeftOrRight = MovementTable[2] - MovementTable[4]

so what I am wondering is how will that be translated into the Cross product

I am also struggling doing this with multiple rays I can only seem to do one ray pointing straight out at the look vector