Enemy Walks From One Wall To Another Changing Move Direction As He Collides With Them In Unity Game @AlexanderZotov
Enemy Walks From One Wall To Another Changing Move Direction As He Collides With Them In Unity Game  @AlexanderZotov
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.
Enemy Walks From One Wall To Another Changing Move Direction As He Collides With Them In Unity GameSimple Clock Puzzle Game Made With Unity SoftwareHow to make a simple coins counter for your Untiy 2d game? Simple tutorial.New Guess The Movie Game Quiz Is Available On Play Market For Free | Download And Enjoy | Unity MadeUnity Tutorial How To Make Simple Puzzle Game | Stacking Rings Pyramid Educational Game For KidsUnity Tutorial How To Create Fuel Meter Feature For Simple Jetpack Clone Android GameHow To Create Simple Portal With Sprite Splitting Feature In 2D Game Made With Unity SoftwareOne More Way How To Create Parallax Scrolling Background Effect For 2D Game Made With Unity SoftwareHow to Make a Simple Score Counter For Your Unity 2D Game? Simple Tutorial.How to make simple Android escape puzzle game with Unity software | Shake Phone Take Key Open DoorHow To Move Game Object Up And Down With UI Buttons In A 2D Game Made With Unity SoftwareUnity Tutorial How To Make Character Move Left Right and Jump In Android Game Using UI Buttons
Alexander Zotov |

Enemy Walks From One Wall To Another Changing Move Direction As He Collides With Them In Unity Game

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER