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 McStag · Aug 01, 2019 at 02:57 AM · 2dspriterendererfliprigidbody.velocity

Unity 2D, Flip Sprite without using a rigidbody

I have made an npc that patrols between random points. A new point is generated, and the enemy's transform is changed to that new point.

I want its sprite to flip based on whether it is moving left or right. I have found examples like this:

 if(myRigidbody.velocity.x >= 0){
    spriteRenderer.flipX = false;
 }
 else {
    spriteRenderer.flipX = true;
 }

But they don't work with my script, even if my npc has a rigidbody. I think its because the rigidbody.velocity.x isnt actually changing, just the transform.position?

So how can I do it without the rigidbody.velocity?

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 I_Am_Err00r · Aug 01, 2019 at 01:51 PM 1
Share

Provide your movement script, but I'm guessing just like you guessed are that you aren't using the unity physics system at all for horizontal movement.


Don't forget to Debug.log things to test why things are broken; you suspect that the velocity in your rigidbody isn't changing, if you suspect that then write try something like this in the future:

  if(myRigidbody.velocity.x >= 0)
 {
     Debug.log(myRigidbody.velocity.x);
     spriteRenderer.flipX = false;
  }
  else 
 {
     spriteRenderer.flipX = true;
  }

And if your suspicion is correct, then it would log the value if it greater than or equal to 0 (which should be true and 0 all the time).

avatar image McStag I_Am_Err00r · Aug 01, 2019 at 06:40 PM 0
Share

Thanks for the tip about Debug.log I always see people use it in tutorials but never think to use it myself. durrr

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by visca_c · Aug 01, 2019 at 02:22 PM

If you are not using rigidbody velocity, you can just find the delta position of your character, then flip the sprite.

 Vector2 lastPos;   
 void Update()
 {
   Vector2 currentPos = transform.position;
   Vector2 deltaPos = currentPos - lastPos; 

 if(deltaPos.x>=0)
      spriteRenderer.flipX=false;
 else
      spriteRenderer.flipX=true;
 
   lastPos = transform.position;
 }


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 I_Am_Err00r · Aug 01, 2019 at 02:31 PM 0
Share

Genius, love it! Excellent little check for side scrolling games, can't believe I have never seen something like this before!

avatar image Vega4Life · Aug 01, 2019 at 03:38 PM 1
Share

I think this is kind of on the right track, but I don't see a reason to check this every frame. The only time we care about flipping, is when we need a new direction. So I would take out what you have and make a new method with it. Then they can call into it when they change directions. Then ins$$anonymous$$d of doing transform.position - lastPos, it would be (waypoint.position - transform.position). Waypoint being where they are going next.


avatar image McStag · Aug 01, 2019 at 06:44 PM 0
Share

Sick idea using change in position over time. tyty

avatar image Jason124 · Jun 06, 2021 at 11:08 PM 0
Share

I'm getting an error CS0034 Operator '-' is ambiguous on operands of type 'Vector3' and 'Vector2' could you help me?

avatar image visca_c Jason124 · Jun 07, 2021 at 09:56 PM 0
Share

I updated the answer, adding one more variable "currentPos" to handle the casting (Vector3 to Vector2), See if it solves your problem.

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

216 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

Related Questions

If I call flipx on the spriterenderer of an image, why doesn't the sprite attached to the image flip? 1 Answer

How the change the sprite image of spritemask according to the sprite image in that object animator? 0 Answers

2D SpriteRenderer not becoming disabled in Build but will work in Editor 0 Answers

Sprite is missing in SpriteRenderer in some prefabs 0 Answers

Changing one keyframe in my animation changes all of them 2 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