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 PeterAS17 · Mar 30, 2014 at 07:44 PM · raycastainavmeshfollow

NavMesh AI: follow only if seen

Hello everyone, As the title says, I want to know how can an enemy with NavMesh AI follow ONLY if it sees the player?

Here is the script for the AI:

var speed : float = 10.0; var player : Transform; private var dir : Vector3; private var dirFull : Vector3;

     function FixedUpdate()
     {
         dir = (player.position - transform.position).normalized;
         var hit : RaycastHit;
      
         if (Physics.Raycast(transform.position, transform.forward, hit, 1)) // 20 is raycast distance
         {
            if (hit.transform != this.transform)
            {
              Debug.DrawLine (transform.position, hit.point, Color.white);
      
              dir += hit.normal * 20; // 20 is force to repel by
            }
         }
      
         // more raycasts   
         var leftRay = transform.position + Vector3(-0.125, 0, 0);
         var rightRay = transform.position + Vector3(0.125, 0, 0);
      
         if (Physics.Raycast(leftRay, transform.forward, hit, 1)) // 20 is raycast distance
         {
            if (hit.transform != this.transform)
            {
              Debug.DrawLine (leftRay, hit.point, Color.red);
      
              dir += hit.normal * 20; // 20 is force to repel by
            }
         }
      
         // check for rightRay raycast
         if (Physics.Raycast(rightRay, transform.forward, hit, 1)) // 20 is raycast distance
         {
            if (hit.transform != this.transform)
            {
              Debug.DrawLine (rightRay, hit.point, Color.green);
      
              dir += hit.normal * 20; // 20 is force to repel by
            }
         }
      
         var rot = Quaternion.LookRotation (dir);
      
         transform.rotation = Quaternion.Slerp (transform.rotation, rot, Time.deltaTime);
      
         transform.position += transform.forward * (2 * Time.deltaTime); // 20 is speed
      
      
     }

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

Answer by Ed unity · Apr 11, 2014 at 08:03 PM

I am not fully certain what you are trying to do in your code, but there is a realtively easy way to do this. Assuming that you have an open area where the AI's view is not obstructed by geometry, you would have an early out and the actual view check. The early out would be if the Player is within sight Range.

So if your Ai can see 20 units, you would calculate the vector to the Player from the AI and then check the magnitude to see if the player is within the 20 units.

if( (player.Transform - ai.Transform).magnitude > 20 ) return;

Then after that you could do a check to see if the player is in view range. So say the player can see 120 degrees in front of him, you would do a check

if( Vector3.Angle(ai.Forward, player.Transform - ai.Transform) <= (120/2) ) // AI can see player

120 is divided by 2 because the AI wants to look on both sides of his forward vector. Thus he can see 60 degress to the right and 60 degrees to the left of his forward vector. This code is optimal for your situation, but like I said will not fully work if objects are in the way. In that case you could do a raycast with the toPlayer vector(player.Transform - ai.Transform) and see if it collides with any objects.

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 PeterAS17 · Apr 13, 2014 at 04:57 PM 0
Share

Thanks, worked! :D

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

21 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

Related Questions

Enemy AI Problem 0 Answers

Why Do Raycasts Ignore NavMesh Agents 4 Answers

AI - Raycast collision and follow player 1 Answer

Set navmesh destination if to object that is in range 1 Answer

Player's last location as a Transform 1 Answer


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