- Home /
Raycast 2d for detecting walls and walking in a set area.
Hi
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 ;-;
Answer by slavo · May 09, 2017 at 11:25 AM
Hello,
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\navmesh they are overlaping and use NavmeshAgent for move.
https://docs.unity3d.com/Manual/nav-BuildingNavMesh.html https://docs.unity3d.com/Manual/nav-CreateNavMeshAgent.html https://docs.unity3d.com/Manual/nav-CreateNavMeshObstacle.html
Alternativly, you can use steering behaviors for movement https://gamedevelopment.tutsplus.com/series/understanding-steering-behaviors--gamedev-12732
Depends what suits you best.
Hi for 2D side scrolling game, steering behavior should work fine.
Can you explain more in detail? I don't understand what I'm supposed to do or if i'm supposed to write a code to go with it or anything. I would send screenshots but they are too big. I set $$anonymous$$y Ai to a navmesh agent and my walls to obstacles, I baked them , but I dont know if I did it right. What do I do after?
I am using 5.6. But when I start my game, the Ai still goes past the walls