Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
1
Question by Soilyman · Apr 08, 2018 at 05:11 PM · 2dmovementaitop down shooterpaths

How to create AI movement for avoiding procedurally generated objects?

I've been struggling with this for a few days trying different approaches but I end up stumbling over myself and losing motivation because I don't know where to start really. I'm trying to create a simple 2D top-down shooter type game where the player has to avoid obstacles and shoot down enemy ships.

 

I would like the enemy movement to behave similarly to the player, but I'm having trouble getting them to avoid obstacles consistently. Currently, they only move horizontally (scrolling backgrounds give the illusion of vertical movement) and movement is based on a single acceleration variable where 0 is stationary and 1 is their maximum speed. Each enemy casts a boxcast forward, and if that boxcast detects an object then their X acceleration will change dependent on the object's distance. So acceleration to the left or right increases the closer an oncoming object is. This works quite well, but does not account for objects to the left or right of the enemy, so enemies will smash into adjacent objects in order to avoid oncoming ones. Here is a terrible diagram to help visualise:

 

I should point out that the ship will only collide with the largest, brightest asteroids; the smaller, darker ones are just background objects.

 

alt text

 

It's probably worth stating that these asteroid objects spawn one at a time at a random X position above screen, and have a slight variance in their spawn intervals; meaning gaps between each asteroid can be slightly wider/narrower than others. There should always be enough of a gap for a ship to fit through between each asteroid however.

 

I currently don't think I'm approaching this the right way and there is a better approach I've not found. I have an idea (which I'm not sure how to implement) that I could create a dynamic path that avoids the asteroids. Maybe I could place a point to the left or right of an asteroid (whichever side has a larger gap) and the ship could accelerate towards that point along it's X axis, once the point is reached a new point in the path would be placed alongside the next asteroid and so on. I have no idea where to begin with trying something like this though.

 

If anyone can offer advice or point me in the right direction to read up on useful practices for AI movement, I'd be very appreciative as it's entirely new to me :)

untitled-1.png (133.3 kB)
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 RLin · Apr 08, 2018 at 07:20 PM

I think the best way to do this would be to use horizontal raycasts/boxcasts to the left and the right in addition to the boxcasts you already have. If either of the raycasts to the left or right hit, then the ai knows not to go in that direction. If they both hit, have the ai accelerate to the side where the hit distance is greater. You could also create a separate collider for the asteroids that does not affect physics but will affect raycasts. This collider could be larger than the actual asteroid, ensuring that the ai always leaves a reasonable margin of space when dodging an asteroid.

Comment
Add comment · Show 1 · 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 Soilyman · Apr 09, 2018 at 04:17 PM 0
Share

This was my first thought, though I did wonder if it was over-complicating things. I think I'll make another attempt with more adjacent boxcasts, but there was still some issues.

 

For example, in the image above, the adjacent boxcast would detect the asteroid to the right and so attempt to accelerate left. The ship is clamped to the screen boundaries and so would collide with the left side of the screen and eventually collide with the onco$$anonymous$$g asteroid above it. In the image above, the ship would ideally accelerate to the right but at a slow enough speed that the asteroid to the right would safely pass by the ship first.

 

Thanks for taking the time to reply, I'll continue trying out adjacent boxcasts and see what I can come up with for now :)

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.

Follow this Question

Answers Answers and Comments

230 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with 2D AI scripting 2 Answers

Simple AI In 2D - C# 1 Answer

2D Platformer Ai Movement Decision Making Problems 0 Answers

enemy AI patrol not working 1 Answer

moving AI up/down if lower/higher in Sector as player 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges