• 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 MI_Inc · Feb 05, 2020 at 08:21 AM · rotationenemy ailocalscale

How do I flip my enemy... One scrpit works, however it shrinks my enemy aswell... The other ones i've commented out tend to make the enemy flip multiple times

 public float speed;
     public float stoppingDistance;
     public Animator anim;
 
     //public Transform enemyGFX;
     private Rigidbody2D rb;
 
     public int damage = 100;
 
    
     private bool m_FacingRight = true;
 
     private Transform target;
 
     public float lookRadius = 10f;
 
     // Start is called before the first frame update
     void Start()
     {
         target = PlayerManager.instance.player.transform;
         rb = GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void Update()
     {
         float distance = Vector2.Distance(target.position, transform.position);
 
         if (distance > stoppingDistance)
         {
             if (distance <= lookRadius)
             {
                 transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
                 anim.SetBool("isWalking", true);
 
                 /*if (rb.velocity.x >= 0.01f)
                 {
                     enemyGFX.localScale = new Vector3(-1f, 1f, 1f);
                 }
                 else if (rb.velocity.x <= -0.01f)
                 {
                     enemyGFX.localScale = new Vector3(1f, 1f, 1f);
                 }*/
 
 
                 if (target.position.x > transform.position.x)
                 {
                     transform.localScale = new Vector3(-1, 1, 1);
                 }
                 else if (target.position.x < transform.position.x)
                 {
                     transform.localScale = new Vector3(1, 1, 1);
                 }
             }
 
         }
 
 
     }
 
     /*private void Flip()
     {
         // Switch the way the player is labelled as facing.
         m_FacingRight = !m_FacingRight;
 
         transform.Rotate(0f, 180f, 0f);
     }*/
 
     void OnDrawGizmosSelected()
     {
         Gizmos.color = Color.red;
         Gizmos.DrawWireSphere(transform.position, lookRadius);
     }
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 unity_ek98vnTRplGj8Q · Feb 05, 2020 at 03:45 PM 0
Share

You say it shrinks your enemy as well... when look at your enemy in the inspector normally, does it have different scale values than 1,1,1? If so you could do something like transform.localScale = new Vector3($$anonymous$$athf.Abs(transform.localScale.x)*-1, transform.localScale.y,, transform.localScale.z);

avatar image logicandchaos · Feb 05, 2020 at 08:52 PM 0
Share

you can use a bool to keep track of the flipping so it doesn't flip multiple times

 bool isFacingRight;
 if(player.position.x > transform.position.x){
 //face right
 if(!isFacingRight){
 transform.localScale = new Vector3(transform.localScale.x*-1,transform.localScale.y,transform.localScale.z);
 isFacingRight=true;
 }
 }
 else if(player.position.x < transform.position.x){
 //face left
 if(isFacingRight){
 transform.localScale = new Vector3(transform.localScale.x*-1,transform.localScale.y,transform.localScale.z);
 isFacingRight=false;
 }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LiamColee · Feb 05, 2020 at 11:12 PM

is it shrinking because you have the scale of the enemy set to something higher than 1 in the editor? if that is the case, try vector3(-transform.localscale.x,transform.localscale.y,transform.localscale.z).

Comment
Add comment · Show 2 · 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 MI_Inc · Feb 06, 2020 at 07:42 AM 0
Share

Thank you very much, I didn't use the code but ins$$anonymous$$d I decided to use an empty Gameobject and put the enemy as a child... Then i rescaled the Gameobject

avatar image LiamColee MI_Inc · Feb 06, 2020 at 03:37 PM 0
Share

That works too XD. glad you figured it out.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

157 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

Related Questions

Problem with enemy rotation 1 Answer

How to write turret aiming for enemy AI 2d side tank? 1 Answer

How can I get my enemy to rotate to look at my character in my 2D game?? 2 Answers

I need help understanding piece of code 2 Answers

Instantiated object not rotating around the center 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