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 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 this 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 think 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 this is not the best way to do this)

 public bool CanSeePlayer()
     {
         RaycastHit hit;
         Vector3 rayDirection = Player.transform.position - transform.position;
 
         if ((Vector3.Angle(rayDirection, transform.forward)) <= fieldOfViewDegrees * 1f)
         {
             // Detect if player is within the field of view
             if (Physics.Raycast(transform.position, rayDirection, out hit))
             {
                 return (hit.transform.CompareTag("Player"));
             }
         }
 
         return false;
     }
 
     public bool CanSeeObject()
     {
         RaycastHit hit;
         Vector3 rayDirection = Player.transform.position - transform.position;
 
         if ((Vector3.Angle(rayDirection, transform.forward)) <= fieldOfViewDegrees * 0.5f)
         {
             // Detect if player is within the field of view
             if (Physics.Raycast(transform.position, rayDirection, out hit))
             {
                 return (hit.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 within the field of view
   if (Physics.Raycast(transform.position, rayDirection, out hit))
    {
     if((hit.transform.CompareTag("Player")){
       lastPlayerSeen = hit.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 ins$$anonymous$$d 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.

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

My NavMeshAgent doesn't move at all!,Why my NavMeshAgent doesn't move at all? 0 Answers

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

Navemeshagent and doors 1 Answer

NavMeshAgent face same direction as destination point 1 Answer

Spreading AI that are lining up for game defficulty 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