- Home /
Enemy AI Running Through Wall With Raycast
Hi!
I have an enemy AI who wanders around my world with random waypoints and when he incounters an object that isn't the player, he dodges it to get around it and keeps going. He also sends out rays to tell if he can or cann't see the player. If that script comes back true, (So he sees the player) he stops wondering and starts "attacking" by moving at the player in a faster motion. Also, if the player isn't behind anything, (wall) he will start firing at the player. But if the player goes behind a wall or something, the enemy stops firing and keeps running at the player. Then when he incounters the wall, he dodges it. Heres the problem. While the enemy is dodging the wall, he is still locked on to the player. So if the player keeps running farther and farther away, the enemy while stop dodging and just lock onto the player and run straight at him through the wall. Any ideas? Comment if you need more information...THANKS!
Answer by DoctorWhy · Jul 17, 2012 at 05:44 PM
Is the AI script written by you? If so, it seems more like a problem with the script, and not a problem that we can understand without a code example.
Have you tried running the code through the Debugger, and putting a break point where the AI should be dodging a wall, but is deciding not to? If you don't know how to use the debugger, I would recommend learning how because it will save you hours of searching for the issue. This looks like a good place to start http://docs.unity3d.com/Documentation/Manual/Debugger.html . The debugger is very similar to other debuggers I used, so if you ever debugged any code, then you should be able to figure this one out.
I have never used a debugger before but it sounds very useful! I didn't need to use it to figure out what needs to be done though. I have got to the point where all I need is a little code telling my AI to go to the position of the player BEFORE he lost sight of him (So where the player was before he went behind "cover"). Any ideas? THAN$$anonymous$$S!
It is hard to say depending on how your AI is programmed. If you are passing it the player and it is using the player position to go to (the gameObject.transform.position), it can just save the players last known position then move to that position using the same chasing logic it would for the player. But again, it depends on how the AI is programmed.
Right right. I wrote the AI script myself. What happens is when the player goes behind "cover" the enemy chases after him still. What I would like is for him to stop chasing the player but start "chasing" the last known position of the player before he went behind "cover. By the way, how can I make my AI save the players position before he "lost him"? If you can tell me that I can get the rest...THAN$$anonymous$$S!
Just save a class scope Vector3, e.g. "LastPlayerPosition", of the known player every Update() and use that LastPlayerPosition to do the moving logic ins$$anonymous$$d of the player's transform.position. Then, just don't update the position if the AI can't see the player, and the AI will run to the LastPlayerPosition ins$$anonymous$$d of the player's transform.position.
OH! Ok. I see exactly what to do know. Well, all but one thing...What is the code to save a vector 3 position. That is all I need and I'm good. Thnaks for all the help!
Your answer
Follow this Question
Related Questions
[SOLVED] using var from another method? 2 Answers
How to stop enemy shooting through wall 1 Answer
How to Stop Enemy "Shooting Through A Wall" 1 Answer
enemy raycast to detect player 1 Answer
Enemy to move out of way of player 1 Answer