• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Aelrift · May 09, 2017 at 05:56 AM · unity 52d

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 ;-;

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aelrift · May 10, 2017 at 01:50 AM 0
Share

Would it work on a side scrolling game

avatar image slavo Aelrift · May 10, 2017 at 08:48 AM 0
Share

Hi for 2D side scrolling game, steering behavior should work fine.

avatar image Aelrift slavo · May 10, 2017 at 02:34 PM 0
Share

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?

Show more comments
avatar image Aelrift · May 10, 2017 at 04:33 PM 0
Share

I am using 5.6. But when I start my game, the Ai still goes past the walls

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2d obj look at limit in unity 0 Answers

How to monitor the movment speed of a 2D sprite 0 Answers

How to make two objects collide without bouncing 2D 0 Answers

Teleport Not Working in Unity 2D 1 Answer

How to prevent jitter with movement over curved slopes 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges