Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by ExtremePowers · Nov 16, 2014 at 01:29 PM · terrainproceduralinfinite

Procedural Object Avoidance

I have a "infinite" procedural terrain, how can I have AI that avoids gameobjects and walks around them to get to the target?

I can't use NavMesh, because it can't bake at runtime.

I am not sure if I can use A* because it seems to work with either predefined paths or a grid system which can't fit an infinite terrain. (I am not sure if it has some way to do this)

If you could give me any suggestions on what would work fine for my game(Minecraft type just without voxels)

Comment
Add comment · Show 3
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 ExtremePowers · Nov 16, 2014 at 03:58 PM 0
Share

Another question, my code looks pretty messy right now, could you help me simplify it? :)

http://pastebin.com/$$anonymous$$n1DTYJ6

avatar image Scribe · Nov 16, 2014 at 04:14 PM 0
Share

your if statements where you don't actually do anythin except contain another if statement could be combined using the '&&' operator:

 if(this){
     if(that){
         doSomething();
     }
 }

could be:

 if(this && that){
     doSomething();
 }

And I don't understand what you are trying to achieve with lines 103-117, you seem to have way too many parenthesise '{}' as you close all your else statements twice (though i haven't actually counted) and you do the equivalent of 'if this whole list of things is true, set target to null, otherwise set target to null' so why bother with the if's at all!

avatar image ExtremePowers · Nov 16, 2014 at 07:48 PM 0
Share

The 103-117 all have a else statement, except the last one because if none of them are met then it should remove the target.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Scribe · Nov 16, 2014 at 01:38 PM

If you know you're obstacles can be avoided by going either way around them, as in they are single blocks of obstacleness, then you could have your enemy walk in your direction and if there becomes an obstacle a certain distance in front then move parallel until the enemy ca 'see' you again.

Alternatively, I think the best bet would be to update you navigation grid proceduaral as you update your terrain or whatever!

Comment
Add comment · Show 4 · 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 ExtremePowers · Nov 16, 2014 at 02:23 PM 0
Share

Yea, that is what I have tried to do, with a last known position, it will then walk to that position and check if the player is there, the problem is I want the AI to go to a specific position, and avoiding trees and houses etc.

avatar image Scribe · Nov 16, 2014 at 02:55 PM 0
Share

here's a very simple example code of what I mean:

 public Transform target;
 public string obstacleTag = "obstacle";
 public float dist = 5;
 public float speed = 1;
 Vector3 dir;
 
 void Update () {
     dir = (target.position - transform.position).normalized;
     dir.y = 0;
     RaycastHit hit;
     if(Physics.SphereCast(transform.position, 0.5f, dir, out hit, dist)){
         Debug.Log(hit.collider.tag);
         if(hit.collider.tag == obstacleTag){
             transform.position += (speed*new Vector3(dir.z, 0, -dir.x)*Time.deltaTime);
         }else{
             transform.position += (speed*dir*Time.deltaTime);
         }
     }else{
         transform.position += (speed*dir*Time.deltaTime);
     }
 }

so you have your enemies target transform which he moves towars while he can, and if he raycasts and hits something with tag "obstacle" he will side step for a while until he can see the object again. Note that I have not handled ignoring collisions with your self in this example so when I was testing I simply deleted my enemies collider, you should set up layer masks for this.

avatar image ExtremePowers · Nov 18, 2014 at 10:12 PM 0
Share

@Scribe This still makes the problem, it should be able to move through a maze, if the player was teleported to the other end.

avatar image Scribe · Nov 19, 2014 at 12:05 PM 0
Share

If you need it to go through a maze you will have to have a grid based system, just update the grid when you update your terrain mesh.

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

27 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

Related Questions

A* Procedural Terrain 1 Answer

How can I get a Low Poly terrain to work in Unity? 2 Answers

How do I go about texturing a flat-shaded generated mesh? 1 Answer

Pixelated Texture Edges on Terrain 0 Answers

Why aren't the mesh collider and mesh filter meshes saved in prefab children? 2 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