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
0
Question by ohkayem · Oct 08, 2017 at 02:46 PM · ai

Simple AI Question

if (Physics.Raycast (ray, out hit)) { if (hit.collider.gameObject.tag == "Player") { Debug.Log ("Hit Player"); navmeshagent.SetDestination(player.position); }I'm sure there's a simple answer to this, but I've been trying to code AI to follow the player when the Raycast emitted from the enemy hit the player. I can code for the enemy to move toward the destination of the player but there are two limitations of this code :

  1. The enemy only ever moves toward the players initial destination and does not follow the player

  2. Once the enemy reaches the players destination, enemy movement will only be initiated when the player walks back through the ray.

In short, I want to trigger enemy movement toward the player when the ray hits the player and the enemy to follow the player endlessly until destroyed.

Thanks!

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 Kishotta · Oct 08, 2017 at 06:31 PM

  1. This code should either be running in Update, or (better) in a coroutine that executes every x (say, 0.1) seconds.

  2. You should make sure the ray is casting in the direction player.position - transform.position. This will make sure that the ray is blocked by obstacles in the same collision layer as the raycasts (by default, everything), essentially giving you line of sight. If you only update the navmesh target position when the player is detected, and do nothing when the player is not hit, your AI will always follow the last known position of the player.

If you just want the enemy to pursue the player regardless of line of sight and all that, then a raycast is completely unnecessary. Just set the navmesh destination to player.position.

Comment
Add comment · Show 2 · 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 ohkayem · Oct 08, 2017 at 06:44 PM 0
Share

Thanks for this - I'm unsure as to how to code the coroutine? @$$anonymous$$ishotta

avatar image Kishotta ohkayem · Oct 08, 2017 at 07:34 PM 0
Share

No problem.

To use a coroutine (which is good for performance in this case), use something similar to the following:

 IEnumerator UpdateAINav (float navRefreshRate) {
     while (player.isAlive) { // or while(true), just so long as we make sure the player exists
         navmeshagent.SetDestination (player.position); // doing the simple version just because
         yield return new WaitForSeconds (navRefreshRate);
     }
 }

Then in Start or Awake do:

 void Awake () {
     StartCoroutine (UpdateAINav (0.1f));
 }

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

119 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

Related Questions

How can I more optimize My AI Script 0 Answers

problem with LookRotation on simple AI 0 Answers

Tennis game like Grand slam tennis 1 Answer

How do I stop an Enemy NavMeshAgent from intersecting the player model? 3 Answers

How to allow multiple nav mesh agents with different slope climbing abilities to use the same nav mesh? 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