How do I make object simply avoid wall while the movement is only forward?

For example, I create a simple object, code it so that the object only moves forward, but I want to add more behavior to it, like the object avoids a wall like this shown image:

The only general solution to this is pathfinding, such as the AI / navigation subsystem in Unity.

Otherwise you could bake special triggers into your level to trigger certain directional behaviors in certain areas.

Yes, but the only thing is that the object will not follow anything or anyone, instead the object will just go by itself and avoid the wall

Or maybe what if I could make it a navmesh agent? just so that the transform destination (the transform object should follow) is always positioned on the ground at a random range, and every time the object reaches the destination, the transform destination should change to its new ground position


Maybe there is some solution for this?

This is the problem.

“Go by itself” isn’t a valid description of an object’s movement.

What you need to do is list the steps the agent must do.

For instance, it isn’t just “avoid the wall”

It’s also “after you pass a wall, return to some horizontal location”

What horizontal location? The middle?

etc.

So there’s missing game design here.

Yeah, I just wanted to check if there was such a movement like that. I’ll try my best to make something.

A NavMeshAgent is a good fit for this problem. It will find an efficient path from where it is to where you want it to go.