# Enemy roam

Updated on January 1, 2018 in
3 on December 31, 2017

Hi guys. I need help with a unity problem. I want to know how to make an object roam between two points.

Like if i want to make an enemy roam between two points what to i do?

• Liked by
on December 31, 2017

What do you mean by roam? Do you mean walk between 2 set points at a fixed speed, random speed, or have the enemy randomly walk around in a square defined by 2 points? All of these build off of each other, but to implement them becomes exponentially more complicated. Though that said, anything that I listed is entirely doable. (ps: I will not be able to respond for a day or 2)

on December 31, 2017

yes i meant  walk between two points at fixed speed

on January 1, 2018

alright, that makes things a lot easier to program. I guess the next set of questions to really understand what you need are: What type of a game are you making (ie a 2d plat former, a 3d game, or a 2d top down game, or something else that I missed)? and what kind of terrain are you expecting to have your enemy travel over?

The first is important to the math to figure out how to apply velocity to your entity. 2d platforms would be the simplest as you’d just need to figure out if the entity should be moving right or left, then apply the velocity based on that. Top down 2d games and 3d games would be nearly identical with the math. With top down 2d and 3d games you would essentially just need to find the ratio of the distances on the x and y axes which would determine how you should apply the velocity. (ex: if you want your enemy to go between (0, 1) and (2, 3) you’d do something like (2-1, 3-0) then divide that by the sum of the sum absolute value of the vector you just found (1, 3), so you’d end up with (1/4, 3/4) if you then multiply that by the velocity you want the entity to move, the entity will go towards (1, 3). The process for having your entity move in 3d space would be exactly the same, just with 3 coordinates instead of 2.)

The second is important to know if you’d need to incorporate rules for what to do if the entity gets stuck on something or needs to jump over something or what not.

ps: I switched from enemy to entity because it is a more general case and these techniques would be useful for pretty much anything you want to have this behavior.