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
2
Question by Solist · Aug 10, 2014 at 02:25 PM · spritesclick to movesprite animation

Sprite animation dependant on object direction

I am making a 2d sprite in a 3d world and have been looking for/thinking of a way to get my 2d sprite (which has a set of images for each of 4 directions) to change which set of images it is using dependent on its current direction that it is either facing/moving (based on the y axis). This is for a click to move by the way, kind of like Ragnarok online.

my thoughts were to find the direction the object is facing and get it to change the animation with if statements.

i.e.

  1. if (playerMove == true){

  2.   if (Lookat.y == >0 && <90) {
    
  3.       playFront() ;
    
  4.   }
    
  5.   if (Lookat.y == >179 && <270) {
    
  6.       playLeft() ;
    
  7.   }
    
  8.   if (Lookat.y == >179 && <270) {
    
  9.       playBack() ;
    
  10.  }
    
  11.  if (Lookat.y == >269 && <1) {
    
  12.      playright() ;
    
  13.  }
    
  14. }

at the current point in time i'm not even sure if i'm using the right approach for this sort of problem.

any input is greatly appreciated.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Derptastic · Apr 02, 2017 at 09:57 AM

I know the thread is old, but I stumbled on it while searching for a solution myself and eventually pieced one together. I hope it helps someone.

 Vector2 me = transform.position \\The object that I want to turn
 Vector2 target = *player*.transform.position \\the target I want to turn to (in my case the player)
 CalculateAngleForAnim(me, target);
     
 private void CalculateAngleForAnim(Vector2 me, Vector2 target)
         {
             float angleBetween = AngleBetweenVector2(me, target);
     
             if (angleBetween >= 45 && angleBetween < 135)
             {
                 //up
             }
             else if (angleBetween >= 135 || angleBetween < -135)
             {
                 //left
             }
             else if (angleBetween >= -135 && angleBetween < -45)
             {
                 //down
             }
             else if (angleBetween >= -45 && angleBetween < 45)
             {
                 //right
             }
         }

  private float  AngleBetweenVector2(Vector2 vec1, Vector2 vec2)
         {
             Vector2 diference = vec2 - vec1;
             float sign = (vec2.y < vec1.y) ? -1.0f : 1.0f;
             return Vector2.Angle(Vector2.right, diference) * sign;
         }

http://answers.unity3d.com/questions/317648/angle-between-two-vectors.html

This answer was invaluable to get it working.

Cheers @fermmmm for the comment.

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 ElowanGames · Aug 12, 2018 at 09:30 AM 0
Share

This was extremely useful for me. Thank you very much!

avatar image
0

Answer by bdickason · Aug 10, 2014 at 03:41 PM

My suggestion would be to attach an animator object and pass in your facing_x and facing_y variables as floats (or ints) to the animator.

You can then create a blend tree in your animator that says 'if facing_x = 0 and facing_y = 1' load the facing_right sprite.

Here's a great forum thread detailing this approach (look at keely's post): http://forum.unity3d.com/threads/animator-state-machine-for-top-down-2d-games-with-non-rotational-sprite-animation.212172/

I also have a code sample in my game that you can see here: https://github.com/bdickason/stormsword/blob/e03e2fc0b896441d880835533e55d28f7f4ac80e/Assets/Resources/scripts/characters/MoveScript.cs

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Sprite Animation - Oneshot 0 Answers

2D Sprite animation: Mesh swap or UV swap? 1 Answer

How do I partially repaint sprite? 0 Answers

2D environment manipulation 0 Answers

Adding texture to cube 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