Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kuwmii · Dec 30, 2014 at 09:29 PM · collidersobject movement

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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 !

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kuwmii · Dec 31, 2014 at 07:23 PM 0
Share

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!

avatar image Kuwmii · Jan 01, 2015 at 08:27 PM 0
Share

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!

avatar image Inan-Evin · Jan 02, 2015 at 03:04 PM 0
Share

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.

avatar image
0

Answer by MiniFish · Jan 02, 2015 at 03:58 AM

what you're looking for is obstacle avoidance behaviour. it'll be a little complicated, but this tutorial video should be able to get you going.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

physics.OverlapSphere colliders 1 Answer

Unity 5 Max colliders in a scene 0 Answers

Collider points one frame old in OnTriggerStay2D 1 Answer

What kind of interpolation for my rigidbody 1 Answer

Scene Loading occasionally breaks Rigid Bodies... 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges