- Home /
Enemy detect object and move around it.
Hello guys,
I am working with C# and I am creating a 2D and need some help. I need a enemy to detect where there is a blocked object "wall" with tag, then move around it. I am pretty new and still learning, if you could help it would be very awesome! :)
I already done the enemy, following a player. What I need is for the enemy to detect a object "wall" collider and move around it. maybe something with oncollisionexit function? stuck on this one!
Something like:
void OnTriggerEnter2D(Collider2D collider){
if (collider.gameObject.tag == "Wall"){
transform.Translate(Vector3.up * 1f * Time.deltaTime);
}
}
Any help please?
Answer by Inan-Evin · Dec 30, 2014 at 09:41 PM
Well since you are doing your "move around" with just translating it up ( assuming you are simulating an enemy jump ) I could offer a solution to detect the wall. Use raycasting.
What can you do is casting a ray in the mid point or whereever you want of enemy, and then if that ray detects a collision, and if that collision is tagged "Wall" you can order enemy to jump.
public bool canJump;
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right);
if(hit.collider.tag == "Wall" && canJump)
{
canJump = false;
transform.Translate(Vector3.up * 1f * Time.deltaTime);
}
now over there, i have a boolean variable called canJump, this is due to fact that raycasting will be done in update or fixedupdate, which means that if you do not disable it after one frame of detection, it will run the code more than one time. Because it will still be casting it forward, which may continue hitting the wall until the enemy gets over the wall, your enemy can translate up more that once, that can lead it to fly to the sky. Now last part is, we need to enable it again, which can be done with different ways :
Use a coroutine. If you have a nearly constant amount of time between enemy jumping and falling back again, once it has jumped, you can call a corouitine, wait for some time, and then activate canJump again to be able to start detecting collisions again.
StartCoroutine(WaitForSomeTime());
IEnumerator WaitForSomeTime(){
yield return new WaitForSeconds(0.5f);
canJump = true;
}
Use a backwards ray, same as above, just use -Vector2.right, and if that hits a wall, meaning that enemy has passed over the wall and can detect a new wall, just make canJump true again.
Use logic, you can simply make canJump true if enemy is grounded, false if enemy is not grounded. It all depends on your game logic.
Or as i said, any other logic that can be use in order to make canJump true / or false, depends on your gameplay. Good luck !
Thanks for you replay dude! I really appreciate it. However, no. I am not simulating an enemy jump, but rather a enemy goes around the object (around the "wall" until he finds a way in)
What I've done; The enemy spawns, goes follow the player to attack him.
What I need; if there is a object in front of the enemy, he goes around it and finds a way through.
I used the statement of "transform.Translate(Vector3.up 1f Time.deltaTime);" which is the a example, I need to make the enemy move around it and enter the zone not actually jump it, thanks for your replay though!
Thanks for you replay dude! I really appreciate it. However, no. I am not simulating an enemy jump, but rather a enemy goes around the object (around the "wall" until he finds a way in)
What I've done; The enemy spawns, goes follow the player to attack him.
What I need; if there is a object in front of the enemy, he goes around it and finds a way through.
I used the statement of "transform.Translate(Vector3.up 1f Time.deltaTime);" which is the a example, I need to make the enemy move around it and enter the zone not actually jump it, thanks for your replay though!
well sorry for misunderstanding, then still the methods that i told you can be used, for moving around it you should search obstacle avoidance. Actually it can be confusing, but since you are working on 2D platform, you may create your obstacle avoidance system by using raycasts, a lot of logic can be applied. When a wall is detected, you may search for an object tagged "avoidance object" or smt like that, which you could place near your walls in order enemy to go, so a wall is detected, than closest avoidance object is detected, then you would make the enemy move towards that object, and then simply go to a position which is front of the wall. You can calculate the front by simply doing this ; once a wall is detected, save your distance to the wall in one axis ( x or z axis, i am confusing them in 2D's but anyways, you understand which axis i mean, the "front") and then add that distance in that axis to wall's position and you have a new position which is in front of the wall. This is a very simple example, you can complicate it with raycasts, or as told before, search for obstacle avoidance there are plenty of examples from basic to advanced.