Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 AlejandroBoss10 · Jun 04, 2019 at 05:53 AM · 2dmovementflipx-axis

Flip enemy based on player direction Unity 2D

Hey there. So what I want is fairly simple I believe. So I have a 2D enemy, and I want it to flip based on where the player is. I simply want the enemy to flip either left or right.

If the player is to the right of the enemy, the enemy should be looking right. If the player is on the left of the enemy, the enemy should be looking left. I would prefer this on the scale and not rotation. In this case, I want the snail to face the direction of the character in the X-axis, either left or right depending on where the player is.

Thank you.

PS: Would prefer it in C#.

alt text

flip-enemy.gif (490.0 kB)
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
2
Best Answer

Answer by SirPaddow · Jun 04, 2019 at 06:20 AM

The SpriteRenderer of your snail has a "flipX" attribute that you can check to flip the sprite, so your code should look something like:

 using UnityEngine;
 
 [RequireComponent(typeof(SpriteRenderer))]
 public class SnailFacePlayer : MonoBehaviour
 {
 
     public Transform playerCharacter;
     private SpriteRenderer spriteRenderer;
 
     public void Awake()
     {
         this.spriteRenderer = this.GetComponent<SpriteRenderer>();
     }
 
     public void Update()
     {
         this.spriteRenderer.flipX = playerCharacter.transform.position.x < this.transform.position.x;
     }
 }
 
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 AlejandroBoss10 · Jun 04, 2019 at 07:47 AM 0
Share

It friggin worked! Thanks. I'm definitely putting you in the credits of my game, that's if you'd like lol. Seriously though, thank you. It would have taken me forever to figure that out.

avatar image housayyn_r · Oct 03, 2020 at 09:51 AM 0
Share

should i change "this" to something else?

avatar image samccloy4 · Jun 12, 2021 at 11:36 PM 0
Share

using System.Collections; using System.Collections.Generic; using UnityEngine;

[RequireComponent(typeof(SpriteRenderer))] public class copfaceplayer : MonoBehaviour {

  public Transform playerCharacter;
  private SpriteRenderer spriteRenderer;
 
  public void Awake()
  {
      this.spriteRenderer = this.GetComponent<SpriteRenderer>();
  }

  
 
 
  public void Update()
  {
      this.spriteRenderer.flipX = playerCharacter.transform.position.x < this.transform.position.x;

         {

if (playerCharacter == null) playerCharacter = GameObject.FindGameObjectWithTag("Player").transform; }

  }

}

avatar image rocamalo · Jul 29, 2021 at 07:00 PM 0
Share

Just exactly what i was looking for my pet that follows my player, thanks!

avatar image abushaheen · Feb 06 at 04:07 AM 0
Share

I managed to flip my characters toward player, but faced a problem the character keeps flipping if it is in the same position of the player, I managed to fix that problem but with so many steps and colliders :(

It's been more than a year now making my game trying to find a fix for that until I found this magic line of @SirPaddow which saved my life. Thank you so much

this.spriteRenderer.flipX = playerCharacter.transform.position.x < this.transform.position.x;

my best regards,,

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

251 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

Related Questions

My sprite is teleporting when I change my scale to -1 to flip it 1 Answer

Rotate/Flip Image in Unity 2d using Virtual Joystick 1 Answer

Sprite flip movement script not working! HELP! 0 Answers

what collider to use for 2d movement? 1 Answer

How do I get my character to keep his left and right momentum in the air while also disabling movement controls until grounded? 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