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
0
Question by superventure · Aug 23, 2014 at 05:04 AM · 2dpositionscreenflipreverse

Flip 2d character left/right

I need to flip a 2d character in a 3d world to look right/left dependent on the direction they move relative to the screen. So if they wander in a left direction relative to the screen it will flip to look left and vice e versa.

How do I define an objects position relative to the screen so I can tell if it moved in a left/right direction from it's last 'relative to screen position'?

Thanks for any help!

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
0

Answer by robertbu · Aug 23, 2014 at 05:41 AM

Assuming you have a standard 2D setup, just subtract the previous position of the object from the current position of the object. If the x of the resulting vector is negative, the movement is to the left. A positive value, and the object is moving to the right.

Comment
Add comment · Show 3 · 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 superventure · Aug 23, 2014 at 12:18 PM 0
Share

$$anonymous$$y set up an npc object nested in an empty gameobject that has the movement script on it. the nested npc is the 2d image that needs to face left/right while pointing at the camera. I've tried this

 TempPos = Camera.main.WorldToScreenPoint(target.position);
 if(lastPos.x - TempPos.x < 0)
 innerNpc.transform.localScale.x = -1;
 
     else 
 inner.transform.localScale.x = 1;  

but it only flips occasionally and still in the wrong direction. What am I missing?

avatar image robertbu · Aug 23, 2014 at 01:59 PM 0
Share

This code fragment does not show how and when you are setting lastPos.

avatar image EverlessDrop · May 09, 2015 at 02:31 PM 0
Share

Try having it so everytime you change direction run this:

 npc.transform.localScale.x *= -1;

I've not tested it but I think that works

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

22 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

Related Questions

UI RectTransform Position && Screen Resolution 2 Answers

Touch.position, Touch.deltaPosition, Screen.width, Screen.Height 1 Answer

Fitting in a turn around animation (Alucard-like) in a transition from facing right to the left (2D) 1 Answer

Flipping an 2D object with joints 2 Answers

Automatically adjusting the camera view 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