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 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.

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

Flip over an object (smooth transition) 3 Answers

Instantiated object not rotating around the center 0 Answers

Enemy rotating to look at next way point unity 3d 1 Answer

How to make an enemy that walks around a platform? 0 Answers

My enemy character is not facing the player correctly! 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