Raycast 2d for detecting walls and walking in a set area.


So we’ve been working a game for some time now, about a month or so (I have to say I’m a complete noob at programming, I’ve never done it before we started this game) But, we have a little problem.
The game is set a little bit like Kingdoms, you’re a king and have to rebuild your castle and battle other people that want your money, to battle them, you have an army of people you can recruit and that sometimes just come to you (AKA villagers) those villagers ca be assigned to certain role BUT depending on that role they’ll move differently. Right now we’re having trouble making the simple unassigned villager walk inside the castle. We already have the code for him, but basically the castle has walls and we want him to walk within to walls. Another but, the player can extend these walls and we want to make the Ai walk within the full range of the walls . So if at the beginning we only have the normal castle walls, and then we build some more walls a=outside , we want him to walk in between the furthest walls. Figured raycasting would be the solution but I have no idea how to do it. Help ;-;


from Unity 5.6 you can use Navmesh for your 2D game. You basicly bake Navmesh, set walls as NavmeshObstacle that carve out par of m
avmesh they are overlaping and use NavmeshAgent for move.

Alternativly, you can use steering behaviors for movement Understanding Steering Behaviors - Envato Tuts+ Game Development Tutorials

Depends what suits you best.