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 jampakdd · Apr 07, 2015 at 01:02 AM · javascriptbugenemyfollow

AI getting stuck on corners

Hi im new to game making and know 0 coding but "Understand" bits and pieces. I am attempting to make a chasing AI that always follows the player but when I turn a corner or in this case a hall with a few openings I turn the corner and it seems to follow me in a strait line instead of following as it should.

The code im using:

 var target : Transform; 
  var moveSpeed = 3; 
  var rotationSpeed = 3; 
   
  var myTransform : Transform; //current transform data of this enemy
   
  function Awake()
  {
      myTransform = transform; //cache transform data for easy access/preformance
  }
   
  function Start()
  {
       target = GameObject.FindWithTag("Player").transform; //target the player
   
  }
   
  function Update () {
      myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
      Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
   
      //move towards the player
      myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
   
   
  }

Screen shots for example:

http://i.imgur.com/Gy72iaU.png

http://prntscr.com/6qidv9

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 betaFlux · Apr 07, 2015 at 03:46 AM

The problem is, you only tell your ai to walk straight ahead towards the player, without taking obstacles into account. I guess what you want is pathfinding. That's where Unity's NavMeshAgent Component comes into play. It takes only four simple steps to make your ai go wherever you want.

Step 1: Make all obstacles in the scene 'static' (tick checkbox in the upper right corner in the inspector of the object) including walls and the ground of course.

Step 2: Click "Windows -> Navigation" and in the Navigation window that appears, click on "Bake" (bottom right corner), this will calculate all walkable areas. Don't forget to hit the bake button again, in case you change any of the navigation values.

Step 3: In addition to your custom ai script add a NavMeshAgent component to your ai character. (found under Component/Navigation/Nav Mesh Agent)

Step 4: Finally do some changes to your code:

 var target : Transform; 
 var moveSpeed = 3;
 var stoppingDistance = 0.1;
    
 var myTransform : Transform; //current transform data of this enemy
  
 private var _navMeshAgent : NavMeshAgent;
 
 function Awake()
 {
     myTransform = transform; //cache transform data for easy access/preformance
     _navMeshAgent = GetComponent(NavMeshAgent);
 }
 
 function Update () {
 
     // make it so you still can handle the speed in the inspector of this component,
     // just for simplicity's sake
     _navMeshAgent.speed = moveSpeed;
 
     // same goes for the distance where the ai should stop infront of the target
     // and any other variables you might want to assign
     _navMeshAgent.stoppingDistance = stoppingDistance;
 
     // this line makes the ai constantly follow the target while avoiding obstacles
     _navMeshAgent.destination = target.position;
    
 }

I hope it helps!

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 jampakdd · Jun 17, 2015 at 04:33 PM 0
Share

Thanks bud! Worked perfectly.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

What’s wrong with my Safe Volume (safe zone) code? 0 Answers

Enemy following Player in range 2 Answers

Enemy not rotating when inside range 1 Answer

Help with Enemy AI 1 Answer

Enemy AI With changing 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