• 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 BMRX · May 09, 2015 at 02:50 AM · raycastainavmeshnavmeshagentfsm

Player's last location as a Transform

So I have my AI moving around my scene using a NavMesh.

If order for the AI to move around it needs a target that is a Transform.

Obviously the Player is the target, but I would like for the player to have the ability to disappear making the AI search in he players last known location.

I assume t$$anonymous$$s can be done by having the AI raycast save the Vector3 of the gameobject with the tag "player" in a variable, then run a method that would have the AI "scan" in and around said area as my AI "sees" with raycasts.

If I'm right, awesome. I just cannot t$$anonymous$$nk of how to do it.

Currently I use these two methods to trick the AI in to stop persuit when it sees a certain tag (I also assume that t$$anonymous$$s is not the best way to do t$$anonymous$$s)

 public bool CanSeePlayer()
     {
         RaycastHit $$anonymous$$t;
         Vector3 rayDirection = Player.transform.position - transform.position;
 
         if ((Vector3.Angle(rayDirection, transform.forward)) <= fieldOfViewDegrees * 1f)
         {
             // Detect if player is wit$$anonymous$$n the field of view
             if (Physics.Raycast(transform.position, rayDirection, out $$anonymous$$t))
             {
                 return ($$anonymous$$t.transform.CompareTag("Player"));
             }
         }
 
         return false;
     }
 
     public bool CanSeeObject()
     {
         RaycastHit $$anonymous$$t;
         Vector3 rayDirection = Player.transform.position - transform.position;
 
         if ((Vector3.Angle(rayDirection, transform.forward)) <= fieldOfViewDegrees * 0.5f)
         {
             // Detect if player is wit$$anonymous$$n the field of view
             if (Physics.Raycast(transform.position, rayDirection, out $$anonymous$$t))
             {
                 return ($$anonymous$$t.transform.CompareTag("Object"));
             }
         }
 
         return false;
     }

So it's implemented as:

 //If tag check = Object, transition to search state
         if (npc.GetComponent<NPCTankController>().CanSeeObject())
         {
             npc.GetComponent<NPCTankController>().target = null;
             Debug.Log("Switch to Search State");
             npc.GetComponent<NPCTankController>().SetTransition(Transition.LostPlayer);
         }

And the opposite in other States:

 //If player seen, switch to chase.
         if (npc.GetComponent<NPCTankController>().CanSeePlayer())
         {
             npc.GetComponent<NPCTankController>().target = player;
             Debug.Log("Switch to Chase State");
             npc.GetComponent<NPCTankController>().SetTransition(Transition.SawPlayer);
         }

What I want is that if CanSeeObject()becomes true, that the AI would take the Vector3 of what CanSeePlayer() was and move on it's own in a certain area "scanning" with CanSeePlayer() from left to right until it sees the player, of course if Player is not found then I just want the AI to wander around randomly until CanSeePlayer() is true again.

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

Answer by siaran · May 09, 2015 at 11:42 AM

So, all you need to do is keep a Vector3 variable that stores the last place the player was seen then?

So you change the code that detects if the player can be seen a bit

   // Detect if player is wit$$anonymous$$n the field of view
   if (Physics.Raycast(transform.position, rayDirection, out $$anonymous$$t))
    {
     if(($$anonymous$$t.transform.CompareTag("Player")){
       lastPlayerSeen = $$anonymous$$t.point;
       return true;
     }
     return false;
  }

That gives you the last seen player location. So then you need to add some code when it switches from true to false to do your scan (either for x amount of time or until it has completed a certain action) and then go back to its normal behaviour if it doesn't find the player (or start chasing the player again if it does).

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 BMRX · May 10, 2015 at 10:48 PM 0
Share

Thanks Siaran, this does not work as fully intended but it does seem to work. When the AI detects an object as the hit.point instead of player it seems to go back to "player last position" that said player last position is never what it actually was.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Checking if a non-walkable layer is blocking the path? (NavMeshAgent) 0 Answers

How do I limit the path of a NavMeshAgent? 0 Answers

How to build a custom shape NavMesh Area 1 Answer

Reference a Navmesh local "island" 2 Answers

Many navmesh agents makes causes them to freeze/creates delay with input 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