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
1
Question by rickycolon9540 · Oct 19, 2021 at 09:17 PM · 2d gameaiprogramming2d sprites

How to flip enemy 2D Sprite

In my game, I have the player castle in the center of the map and I have enemy spawn positions at the edge of the map. I am trying to find a way to make the enemy sprite face the direction they are moving in (left or right), I know about the "Flip" mechanic on the Sprite Renderer but all the tutorials I find are based on player input and none actually grab the direction of the gameobject.

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
Best Answer

Answer by AbdeCodeEnjoyer · Oct 20, 2021 at 03:47 AM

@rickycolon9540 If you want this script to run on each enemy instance, you could do something like this

  public float speed;
  public Transform target;
  public SpriteRenderer spriteRenderer;

 void Update ()
 {
   //direction of the target depending of this object
   Vector2 direction = (target.position - transform.position).normalized; 
   //supposing that your object is initially looking right
  spriteRenderer.flipX = direction.x < 0;
  //if you are using another method of movement like rigidbody then do the same but with 
  //rigibody.velocity = direction * speed * Time.deltaTime
   transform.position += direction * speed * Time.deltaTime;
 }


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 AbdeCodeEnjoyer · Oct 20, 2021 at 03:52 AM 0
Share

Additionally if your target does not move, you can remove the first two lines in the update callback and put them in the start callback. (Therefore running your game a little faster since your target wont be moving)

avatar image rickycolon9540 · Oct 21, 2021 at 04:33 AM 0
Share

This worked beautifully! Thank You!

avatar image ATP-254 · Dec 20, 2021 at 10:21 PM 0
Share

So where could i fit that in my script?

public class Enemy : MonoBehaviour {

 float Speed = .5f;
 bool Left = false;
 bool Right = false;

 // Use this for initialization
 void Start()
 {
      Left = true;

}

// Update is called once per frame void Update() {

 if (Left == true)
 {
     transform.Translate(Vector3.left* Speed * Time.deltaTime);
 }
 if (Right == true)
 {
     transform.Translate(Vector3.right* Speed * Time.deltaTime);
 }

}

void goRight() { Left = false; Right = true; }

void goLeft() { Left = true; Right = false; }

 private void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.tag == "PatLeft")
     {
         goRight();
         
     }
     if (collision.gameObject.tag == "PatRight")
     {
         goLeft();
        

     }

     if (collision.gameObject.name == "flame")
     {
         Destroy(gameObject);
         Destroy(collision.gameObject);
     }

 }


 

}

avatar image AbdeCodeEnjoyer ATP-254 · Dec 25, 2021 at 02:43 AM 0
Share

Your code already seems to work, All i can see that you could modify is the use of the two variables doing the same thing Instead of using two variables controlling your enemy's direction, I propose that you use only one variable called "goRight". Instead of having two conditions in the update you could have only one and write instead something like the following:

 transform.Translate(goRight ? Vector3.right : Vector3.left * speed * Time.deltaTime);

As for the collision part it's already pretty well done you would just need to modify goRight method to something like:

//therefore having only one method didctating the value of the direction

 void setDirection(bool isRight){goRight = isRight;}

then in the collsion part ounce again you would just have to reWrite it to something like

 if (collision.gameObject.tag == "PatLeft")
         setDirection(true);
 
      if (collision.gameObject.tag == "PatRight")
          setDirection(false);
         
 
      if (collision.gameObject.name == "flame")
      {
          Destroy(gameObject);
          Destroy(collision.gameObject);
      }

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

259 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

Related Questions

Multiple Cars not working 1 Answer

How do I make it so that a walking animation plays for the enemy AT THE SAME TIME it is moving toward the player? 0 Answers

Making an FPS arcade game. 0 Answers

Rigidbody2d Floating Above Ground 5 Answers

Generate a new sprite from painting 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