- Home /
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.
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).
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
Follow this Question
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