Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Allurance · Apr 25 at 03:19 PM · 2d2d game2d spritesenemy ai2dplatformer

Enemy Keeps flipping on X axis

I'm trying to have the enemy follow the player and flipping the sprite to keep the enemy facing the player. However, when the player is on top of the enemy, they're neither on the left nor right of the enemy so the enemy starts flipping erratically. Here's my code trying to fix the issue, but it doesn't work:

 private void ChasePlayer()
     {   
         // left side of player
         if (transform.position.x < player.position.x)
         {
             rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
             transform.localScale = Vector3.one;
             isFacingLeft = false;
         }
         else if (transform.position.x > player.position.x) //right side of player
         {
             rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
             transform.localScale = new Vector3(-1f, 1f, 1f);
             isFacingLeft = true;
         } else
         {
             transform.localScale = Vector3.one;
         }
     }

Any help would be appreciated, thank you in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by landings · Apr 25 at 03:42 PM

You can limit their response frequency.

 private float startTime = 0;
 private void ChasePlayerWithLimitedFrequency()
 {
     if (Time.time - startTime < 1f) // 1 second limit
         return;
 
     startTime = Time.time;
 
     // left side of player
     if (transform.position.x < player.position.x)
     {
         rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
         transform.localScale = Vector3.one;
         isFacingLeft = false;
     }
     else if (transform.position.x > player.position.x) //right side of player
     {
         rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
         transform.localScale = new Vector3(-1f, 1f, 1f);
         isFacingLeft = true;
     } 
     else
     {
         transform.localScale = Vector3.one;
     }
 }
Comment
Add comment · Show 5 · 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 Allurance · Apr 25 at 03:55 PM 0
Share

I'm still relatively new to Unity, could use explain how your if statement works a little more in detail please?

avatar image landings Allurance · Apr 26 at 06:03 AM 1
Share

startTime records the starting time of every period. Time.time gives you the current time. Time.time - startTime < 1f means, if current time is within 1 second delay of your recorded start time, this function is not allowed to run. Every time it is allowed to run, it will prepare for the next period by setting a new start time startTime = Time.time. private float startTime = 0; determines if this function is allowed to run in the first few frames. 0 value means it may not allowed to run in the first few frames, great negative value such as -100 means it is allowed to run.

avatar image Allurance landings · Apr 26 at 10:21 PM 0
Share

So we're subtracting the current time from the time when the method was fired? And then checking if the difference equals the time we set for the delay?

Show more comments
avatar image Bunny83 · Apr 27 at 06:57 AM 0
Share

Note: You probably should remove the final else case. It doesn't make much sense the way it is. First of all the chance that this case is even hit is extremely small because the chance that the two x positions are exactly equal is very small. However if it is hit it means you're just flipping the enemy to the right but you don't change the velocity or your "facing" boolean. So it creates an inconsistency for absolutely no reason. The best fix is to remove the final else block and just make one of the two conditions <= or >= so it also covers that rare case. Though since this wouldn't happen very often (especially when you now restrict the response time) just removing the else would do it.

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

329 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Image swapping sprites delay. 0 Answers

Tilemap disappearing from game-view after using SetActive 1 Answer

Why are my pixel art black in game window but not in scene windows? 2 Answers

How to crop sprite during runtime? 2 Answers

Changing the orthographicSize of the camera to make sure play field fits 0 Answers


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