Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 maksymdev · Jul 27, 2016 at 07:17 PM · aienemyplatformerfollow player

Enemy chase player and do not stop until it reaches the target?

Hey, I will get to the point : I have script that makes Enemy move towards my Player (it is 2D platformer). Enemy move if distance between Player and object is less than max dist. but if I reach higher value with my Player than max dist Enemy stops following.

  if (Vector2.Distance(transform.position, Player.transform.position) <= 10)
         {
             Vector2 enemyVelocity = new Vector2((transform.position.x - Player.transform.position.x) * Speed, 0);
             rgBody2d.velocity = -enemyVelocity.normalized * Speed;
             anim.SetFloat("Speed", enemyVelocity.magnitude);
 
         }

How can I make follow my player if it shows in Camera and do not stop until it reaches the Player? Anyone who can explain?

Comment
Add comment · Show 2
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 EDevJogos · Jul 27, 2016 at 11:54 PM 0
Share

Hmm, not sure if i got what you trying to do, but if you just want the enemy to follow the player forever afeter it saw him, you could set a boolean to true if the distance is less then your 10, if this boolean is true, the enemy will move towards the player no matter the distance.

 private bool followPlayer = false;
 
 if (Vector2.Distance(transform.position, Player.transform.position) <= 10)
 {
     followPlayer = true;
 }
 
 if(followPlayer)
 {
     //$$anonymous$$ove towards the player.
 }

avatar image maksymdev EDevJogos · Jul 28, 2016 at 08:16 AM 0
Share

Hi, thanks for your answer! I've already tried this solution earlier but it doesn't work. If think this is because it sets followPlayer to true if the distance is <= 10 so if the distance is greater than 10 it sets followPlayer to false and the second if statement doesn't work.

Any other suggestions? Thank you again.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by mrpmorris · Jul 28, 2016 at 08:33 AM

In your behavior add the following

 void OnBecameVisible() {
   enabled = true;
 }
 
 void OnBecameInvisible() {
   enabled = false;
 }

If that isn't sufficient (because the sprite gets hidden when on screen) try checking the sprite's position against one of the following

  • Screen.width

  • Camera.pixelWidth

Comment
Add comment · Show 4 · 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 maksymdev · Jul 28, 2016 at 07:00 PM 0
Share

I did something like this but it doesn't seem to work..

     void OnBecameVisible()
     {
         enabled = true;
     }
 
     void OnBecameInvisible()
     {
         enabled = false;
     }
 
 
     void chasePlayer()
     {
         Vector2 enemyVelocity = new Vector2((transform.position.x - Player.transform.position.x) * Speed, 0);
         rgBody2d.velocity = -enemyVelocity.normalized * Speed;
         anim.SetFloat("Speed", enemyVelocity.magnitude);
     }
 
     void enemyPatrol()
     {
         if (transform.position == patrolPoints[currentPoint].position)
         {
             transform.localScale = new Vector2(-transform.localScale.x, transform.localScale.y);
             currentPoint++;
         }
         if (currentPoint >= patrolPoints.Length)
         {
             currentPoint = 0;
         }
         transform.position = Vector3.$$anonymous$$oveTowards(transform.position, patrolPoints[currentPoint].position, moveSpeed = Time.deltaTime);
         anim.SetFloat("Speed", transform.position.magnitude);
     }
 
     void Update()
     {
 
         if (enabled)
         {
             chasePlayer();
         }
 
 
         if (!enabled)
         {
             enemyPatrol();
         }
avatar image mrpmorris maksymdev · Jul 28, 2016 at 07:06 PM 0
Share

"It doesn't work" isn't going to tell anyone enough to help you

avatar image maksymdev mrpmorris · Jul 28, 2016 at 07:09 PM 0
Share

Yeah, my bad, sorry. So I tried to read docs about OnBecameVisible but I am not sure how to use it. Is it possible to take Screen.width and check if my Enemy is somewhere between left edge and right edge of the screen?

Show more comments
avatar image
0

Answer by maksymdev · Jul 28, 2016 at 07:58 PM

@mrpmorris so I got this working for me :D

 float leftBorder = Camera.main.ViewportToWorldPoint(new Vector2(0, 0)).x;
         float rightBorder = Camera.main.ViewportToWorldPoint(new Vector2(1, 0)).x;
 
         if (leftBorder <= transform.position.x && rightBorder >= transform.position.x)
         {
             chasePlayer();
         }
         else
         {
             enemyPatrol();
         }

Thank you very much for help and navigating me!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make a enemy follow me 3 Answers

Basic enemy AI for 2.5D Platormer 1 Answer

AI & pathfinding for 2D platformer - how to approach? 0 Answers

How do I have multiple AI that follow the player but don't move into each other? 1 Answer

Make player not be seen by AI, when player in foilage and shadows. 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