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 Michael 12 · Apr 27, 2011 at 07:09 PM · aipathfindingcollisiondetection

AI Avoidance Problem

OK this is is kinda working the way I want it to. I place this as an Update Function in my Alien Zombie AI script:

function Update (){

 //The direction vector to our target
 var dir = (target.position - transform.position).normalized;
 var hit : RaycastHit;
 // Check for forward raycast
 if(Physics.Raycast(transform.position, transform.forward, hit, 20)){
     if(hit.transform != transform){
         Debug.DrawLine(transform.position, hit.point, Color.red);
         dir += hit.normal * 50;
 }

}

var leftR = transform.position; var rightR = transform.position;

leftR.x -= 2; rightR.x += 2;

if(Physics.Raycast(leftR, transform.forward, hit, 20)){ if(hit.transform != transform){ Debug.DrawLine(leftR, hit.point, Color.red); dir += hit.normal 50; } } if(Physics.Raycast(rightR, transform.forward, hit, 20)){ if(hit.transform != transform){ Debug.DrawLine(rightR, hit.point, Color.red); dir += hit.normal 50; } }

var rot = Quaternion.LookRotation(dir);

transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime); transform.position += transform.forward 1 Time.deltaTime;

}

My Alien Zombies are moving around my terrain a lot better and seem to be able to move around trees a lot better. But they pass through my buildings to get at my FPS player. The video tutorial I followed mentioned that the wall needed to have a box collider on it and I understand the reason for that. But is there a way to get this to work with my "Mesh Colliders" that are already assiged to each part of my buildings?? I would hate to have to go around and add an additional box collider to every part of every structure that they could possibly pass through ;)

EDIT

Morten Do you mean like this:

function Update (){

 //The direction vector to our target
 var dir = (target.position - transform.position).normalized;
 var hit : RaycastHit;
 // Check for forward raycast
 if(Physics.Raycast(transform.position, transform.forward, hit, 20)){
     if(hit.transform != transform){
         Debug.DrawLine(transform.position, hit.point, Color.red);
         dir += hit.normal * 50;
 }

}

var leftR = transform.position; var rightR = transform.position;

leftR.x -= 2; rightR.x += 2;

if(Physics.Raycast(leftR, transform.forward, hit, 20)){ if(hit.transform != transform){ Debug.DrawLine(leftR, hit.point, Color.red); dir += hit.normal 50; } } if(Physics.Raycast(rightR, transform.forward, hit, 20)){ if(hit.transform != transform){ Debug.DrawLine(rightR, hit.point, Color.red); dir += hit.normal 50; } }

var rot = Quaternion.LookRotation(dir);

var sizeOfMonster = 1.5; var groundOffset = Vector3.up; var newPosition = transform.position + transform.forward 1 Time.deltaTime; if (!Physics.CheckSphere(newPosition+groundOffset, sizeOfMonster)){ transform.position = newPosition; }

transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime); transform.position += transform.forward 1 Time.deltaTime;

}

I'm going to go out on a limb and ask if the "Monster Size" determines if they can pass through a wall or not, is that correct?

EDIT here is an image of how things are set up and hopefully it demostrates what my Alien Zombies are doing wrong. I added the debug code so I was much better able to adjust the parameters and center the sphere but as you can see they still just waltz right through my structures?? alt text

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
1
Best Answer

Answer by Mortennobel · Apr 27, 2011 at 07:22 PM

It's not easy to write a good AI code. But often a simple AI script is good enough as the one you have.

I suggest one simple addition to your script:

Only update the position to the new value if it doesn't collide with anything (Use Physics.CheckSphere(...) to check for a potential collision).

In code it would be something like (sizeOfMonster and groundOffset should be replaced with variables for your game):

var sizeOfMonster = 1.0;
var groundOffset = Vector3.up;  
var newPosition = transform.position + transform.forward * 1 * Time.deltaTime;
if (!Physics.CheckSphere(newPosition+groundOffset, sizeOfMonster)){
transform.position = newPosition;
}

If you want more advanced AI you should take a look at Path or Behave ( http://angryant.com/ ). Or take a look at my blog about A* path finding ( http://blog.nobel-joergensen.com/2011/02/26/a-path-finding-algorithm-in-unity/ ).

Edited 28th of May: To help debugging the size and position of the sphere use the following code snippet. This will draw the sphere-test collider in the scene when the GameObject is selected in the navigator:

function OnDrawGizmosSelected () {
    Gizmos.color = Color.red;
    var sizeOfMonster = 1.0;
    var groundOffset = Vector3(0,0.5,0); 
    Gizmos.DrawWireSphere (transform.position+groundOffset, sizeOfMonster);
}
Comment
Add comment · Show 22 · 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 Michael 12 · Apr 27, 2011 at 07:41 PM 0
Share

Thanks $$anonymous$$orten, Where would i add this to my existing script? Oh I tried downloading angry ants package but it seemed to conflict with the detonator pack as I got an error message, can't remember what it was but when I unistalled it it went back to normal so I was unfortunately never able to test out the Angyant solution :(

avatar image Mortennobel · Apr 27, 2011 at 08:18 PM 0
Share

$$anonymous$$y code snippet should replace the last line of your script (transform.position += ...).

avatar image Michael 12 · Apr 27, 2011 at 08:25 PM 0
Share

Thanks $$anonymous$$ornet, I updated my Code in my question, does that look right. They still seem to pass through my game objects though, I did tinker with the monster size and it seemed like it had some effect??

avatar image Mortennobel · Apr 27, 2011 at 08:52 PM 0
Share

What is the height of your monsters? $$anonymous$$aybe the groundOffset needs to be different - like Vector3(0,0.5,0) or so. Basically what my addition to the script should do, is to make the moster stop, if it is about to collide with something. ($$anonymous$$aybe you need to do something different - like change direction)

avatar image Michael 12 · Apr 27, 2011 at 08:55 PM 0
Share

The size of my Alien Zombies is 1.0 Wnat's the ground Offset and where would I change that?

Show more comments

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

No one has followed this question yet.

Related Questions

Evade a Collider on Vector3.forward 3 Answers

Entegrating AIFollow.cs and Wayfinder.js (A* Pathfinding Aron) 0 Answers

dynamic node driven room based AI 1 Answer

Movement around a huge object by avoiding obstacles 1 Answer

Have falling object exit from a collider after collision? 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