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
5
Question by RyuMaster · Feb 02, 2014 at 08:04 PM · 2drotationsprite

Rotate 2D Sprite Towards Moving Direction

Hi there! This question has been answered, the solution is to use parent GameObject, but in my case, I can't do it, as I'm using specific GUI sprite.

I have this code to move it along the path and rotate:

 iTween.MoveTo (this.gameObject,iTween.Hash("oncomplete", "SeekNewPlace",    "path",iTweenPath.GetPath("ButterflyPath"), "time", 10f, "easetype", iTween.EaseType.linear));

 void Update()
 {
     Vector3 moveDirection = this.gameObject.transform.position - _origPos; 
     if (moveDirection != Vector3.zero) 
     {
         this.gameObject.transform.rotation = Quaternion.LookRotation(moveDirection);
         // and afterward, if you want to constrain the rotation to a particular axis- in this case Y:
         this.gameObject.transform.eulerAngles = new Vector3(0f, 0f, transform.eulerAngles.x -90f);
 }
 }

But no matte what, sprite is not facing correct direction for me. Maybe there is something else I could use instead of Quaternion for this 2D sprite case?

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

3 Replies

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

Answer by robertbu · Feb 02, 2014 at 08:12 PM

Assuming you are using a sprite viewed from a camera with rotation (0,0,0) and the sprite's right side is considered the 'forward' of the sprite, you can do it this way:

 void Update()
 {
     Vector3 moveDirection = gameObject.transform.position - _origPos; 
     if (moveDirection != Vector3.zero) 
     {
         float angle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
     }
 }

Note '_origPos' is never being updated in this code. Not sure if this is by design or an oversight.

Comment
Add comment · Show 11 · 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 RyuMaster · Feb 03, 2014 at 12:19 PM 0
Share

O$$anonymous$$G THAN$$anonymous$$S! It works now.

avatar image Karwoch · Sep 17, 2014 at 02:28 PM 0
Share

Really, really it works, thanks :D

avatar image Ash-Blue · Nov 14, 2014 at 01:06 AM 0
Share

Note that this works until a parent object's scale is flipped. Using transform.localRotation seems to have no effect :(

avatar image Captain-Awesome · Mar 11, 2015 at 04:37 PM 0
Share

Very nice! I was trying to create the Quaternions directly, but had no idea how to do left and right. But I got up and down!!

// up this.transform.rotation = new Quaternion(0,0,0,0); // down this.transform.rotation = new Quaternion(0,0,180,0);

avatar image duykt3 · Jun 26, 2015 at 07:51 AM 0
Share

Thanks Very much!

Show more comments
avatar image
14

Answer by Jokeman258 · Mar 01, 2015 at 01:21 AM

Use this instead. It's a simpler version specifically for 2D use so you don't have to calculate the _origPos

   void Update () {
         Vector2 moveDirection = gameObject.rigidbody2D.velocity;
         if (moveDirection != Vector2.zero) {
             float angle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
             transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
         }
     }






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 HunterAugust · Aug 08, 2015 at 07:55 PM 0
Share

This helped me out a bit on my project, So thank you sir!

avatar image Sacredfice · Feb 03, 2016 at 12:49 AM 1
Share

This code doesnt work.

avatar image dead_byte_dawn Sacredfice · Apr 22, 2017 at 07:58 AM 0
Share

same, I keep getting a conflict with the float-fader and Quaternion.identity.

avatar image d2clon · Aug 24, 2021 at 07:16 PM 0
Share

Worked for me out of the box

avatar image
0

Answer by kdawgfsho04 · Nov 27, 2020 at 08:43 PM

how do i get it so my sprite doesnt lay sideways when i do this i want my sprite to stay upright when i try to lock in rigid body i get no luck when it changes position it lays sideways i want to keep him straight up anyway to do thiis?

Comment
Add comment · Show 1 · 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 bizleycraft · Feb 18, 2021 at 10:04 PM 0
Share

If you have a sprite that is looking up instead of right, use robertbu's answer and simply subtract 90 from Mathf.Rad2Deg, e.g:

float angle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg - 90;

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

31 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

Related Questions

2D sprite rotation with velocity and interpolation 1 Answer

Change Sprite Origin in 2D 1 Answer

Sprite is a little of when rotating 1 Answer

(solved) 2d sprite rotation towards mouse 1 Answer

how do i make an object always face the player? 5 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