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 dre38w · Apr 29, 2011 at 02:40 PM · collisionaicharactercontroller

How to prevent team AI from moving through everything?

Hello! I am making some AI and I have my teammate AI following the player with this code.

var targetFollow : Vector3 = player.TransformPoint(Vector3(4, 0, -6));
                transform.position = Vector3.SmoothDamp(transform.position, targetFollow, velocity, dampRot);
                transform.rotation = Quaternion.Slerp(transform.rotation, player.rotation, Time.deltaTime * 10);

That works great for following the player the way I want it to. The only issue now is the team AI moves through everything. I'm using a character controller so when the AI needs to branch off and attack an enemy they can move smoothly by use of the character controller. Any idea as to how I can fix the moving through?

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 Joshua · Apr 29, 2011 at 02:53 PM

You could use Spherecasting to see if there is something in front of the AI. If there is something in front of him and distance is less than x have him move a bit to the left/right and check again.

Here is an example I found checking if something is above the character:

function Update () {
    var hit : RaycastHit;
    var charCtrl : CharacterController = GetComponent(CharacterController);
    var p1 : Vector3 = transform.position + charCtrl.center + 
                Vector3.up * (-charCtrl.height*0.5);
    var p2 : Vector3 = p1 + Vector3.up * charCtrl.height;
    // Cast character controller shape 10 meters forward, to see if it is about to hit anything
    if (Physics.CapsuleCast (p1, p2, charCtrl.radius, transform.forward, hit, 10)) {
        distanceToObstacle = hit.distance;
    }
}
Comment
Add comment · Show 8 · 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 dre38w · Apr 29, 2011 at 04:16 PM 0
Share

Thank you for the reply but is there any way to have a collision ins$$anonymous$$d of just moving around it?

avatar image Nevellin · Apr 29, 2011 at 05:03 PM 0
Share

I believe theres a way to make the AI create a path before moving, but I dont know how. If you make him test a collision and then go Left/Right he'll look like a blind man.

avatar image dre38w · Apr 29, 2011 at 06:58 PM 0
Share

Yeah. The thing is the way the AI follows the player around is they float over the player's shoulders. So basically they move and rotate wherever the player moves and rotates. Like little body guards. Just when the player rotates too close to a wall or any object the AI moves straight through like it's air. I have the AI detecting collisions while branched off and chasing enemies but that only checks for things ahead of the AI not in any other direction. And at that point they aren't moving through objects due to deactivating the follow commands and using Character Controller. Thoughts?

avatar image Joshua · Apr 29, 2011 at 07:31 PM 0
Share

Child an empty gameobject to the AI. Reset it's transform. Add a capsule collider and scale it. Through code, turn it on/off when following/not following.

avatar image dre38w · Apr 29, 2011 at 11:13 PM 0
Share

Alrighty. Simple solution. I'll try it and let you know how it goes. Thank you!

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

NavmeshAgent vs Character Controller = Absolute Unity Failure? 1 Answer

Determining rotation for collision avoidance? 3 Answers

Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer

Problem with character controller and collisions 1 Answer

How can I make two Character Controllers Collide 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