Uploaded May 2021 | Updated September 2026, 3 weeks ago
#unityenemymove #unityenemyai #unityenemywalk
In this video I give you a simple script that allows your enemy to walk from one wall to another back and forth changing its direction after collision. So enemy turns around when he touches the wall and starts moving in opposite direction. It is a kind of very simple enemy AI.
I play games here - twitch.tv/zzotoff
You are very welcome :-)
Project files are here
drive.google.com/file/d/1X63GR7IgeNbCXvLyQSwkgLDY_BDFcvcZ/view?usp=sharing
I start from an empty project and the first thing to do is to add an enemy. Here it is. Enemy needs a collider to collide with a wall. This collider will be a trigger. Also to move and to detect collisions the enemy needs a rigid body 2d. I set gravity scale to 0 so enemy will not fall down. Next game object is a wall. Here is one. I position it at the right side of the screen. Right here. Wall needs a collider as well. I set it as a trigger. Wall will have a script which is just an empty one. This script helps me to make sure that enemy collides with wall. I could use a tag for that purpose but I prefer to use scripts. I D&D this script to wall. Duplicate wall gameobject and position that clone at the left side of the screen. Ok.
Final thing is to check out the enemy script. Here I have some variables which help me to move my enemy and to flip its sprite when the enemy moves to the left or to the right. In Start I get localScale and rigitbody, set dirX to negative one so enemy walks to the left initially and set move speed to 3. Ontrigger enter method fires up when enemy collides with something. If that game object has Wall script attached then dirX variable changes its value to opposite one. And in fixed update I give a velocity to rigidbody depending on that dirX value so the enemy walks left or right. And finally in Late Update I flip enemies sprite depending on where my enemy is heading to. Maybe this approach is a bit overkill but it works pretty well for me everytime. That’s the script. I D&D it to enemy. Now I can hit play and see how it works. Enemy walks from one wall to another facing the direction he goes. You can find project files by the link in the description. Thank you for watching and see you next time.
#unityenemymove #unityenemyai #unityenemywalk
In this video I give you a simple script that allows your enemy to walk from one wall to another back and forth changing its direction after collision. So enemy turns around when he touches the wall and starts moving in opposite direction. It is a kind of very simple enemy AI.
I play games here - twitch.tv/zzotoff
You are very welcome :-)
Project files are here
drive.google.com/file/d/1X63GR7IgeNbCXvLyQSwkgLDY_BDFcvcZ/view?usp=sharing
I start from an empty project and the first thing to do is to add an enemy. Here it is. Enemy needs a collider to collide with a wall. This collider will be a trigger. Also to move and to detect collisions the enemy needs a rigid body 2d. I set gravity scale to 0 so enemy will not fall down. Next game object is a wall. Here is one. I position it at the right side of the screen. Right here. Wall needs a collider as well. I set it as a trigger. Wall will have a script which is just an empty one. This script helps me to make sure that enemy collides with wall. I could use a tag for that purpose but I prefer to use scripts. I D&D this script to wall. Duplicate wall gameobject and position that clone at the left side of the screen. Ok.
Final thing is to check out the enemy script. Here I have some variables which help me to move my enemy and to flip its sprite when the enemy moves to the left or to the right. In Start I get localScale and rigitbody, set dirX to negative one so enemy walks to the left initially and set move speed to 3. Ontrigger enter method fires up when enemy collides with something. If that game object has Wall script attached then dirX variable changes its value to opposite one. And in fixed update I give a velocity to rigidbody depending on that dirX value so the enemy walks left or right. And finally in Late Update I flip enemies sprite depending on where my enemy is heading to. Maybe this approach is a bit overkill but it works pretty well for me everytime. That’s the script. I D&D it to enemy. Now I can hit play and see how it works. Enemy walks from one wall to another facing the direction he goes. You can find project files by the link in the description. Thank you for watching and see you next time.










