• 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
1
Question by Fulcrum_Games · Feb 24 at 04:47 AM · movementaimtop down shooterjoysticks

How to determine 2D Directional animation based on player direction in a top down game

alt text

So this is difficult to explain but I'll try. As you can see in the attached image, I am using a blend tree to determine the animations for the player in a top down game. So if the player is moving straight, MoveX should be 1 and MoveZ should be 0, and similarly for the other directions the character could move. All of this works when the player is facing to the top of the screen. However, when the player turns anywhere but the top of the screen, the animation isn't in sync with the direction the player is actually moving.


For instance, if the player is turned to the right of the screen, its Y rotation is 90. I basically want to make it so the way the player is facing should determine where MoveX and MoveY start at (0,1) respectively. This way if the player is facing right (y = 90), and moving to the right side of the screen, MoveX and MoveY should be (0,1) so the animation shows the player going forward. Then, if player faces right but moves to the left of the screen, the animation should be running backwards which would be MoveX = 0 and MoveZ = -1. Right now this would be MoveX = -1 and MoveZ = 0 so the animation is running left.

Here is my function for movement thats called every frame. It uses an on-screen joystick or gamepad left stick to move the player. Im looking for what i should set anim.SetFloat("MoveX", ...); to for the correct animation to play (and likewise for MoveZ).

     void MovementUpdate()
     {
         Vector2 movementInput = playerInput.PlayerControls.Movement.ReadValue<Vector2>();
         Vector3 move = new Vector3(movementInput.x, 0f, movementInput.y);
         controller.Move(move * Time.deltaTime * playerSpeed);
 
         float joystickAmount = Mathf.Max(Mathf.Abs(movementInput.x), Mathf.Abs(movementInput.y));
         anim.SetFloat("MoveX", movementInput.x);
         anim.SetFloat("MoveZ", movementInput.y);
         //Debug.Log(movementInput);
         Debug.Log(gameObject.transform.rotation.y);
 
         if (move != Vector3.zero)
         {
             gameObject.transform.forward = move;
         }
     }



I also don't know much about blend trees so if there's a different blend type I could do that would easily implement player direction in with the X and Z of the joystick, that would be great.

screenshot-2022-02-23-222720.png (111.4 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
1
Best Answer

Answer by haruna9x · Feb 24 at 07:37 AM

You need to convert the movementInput vector in the direction of the character's face (take Vector2.up as the default direction). Use the converted vector to send to the Animator.

 private Vector2 ConvertMovementInput(Transform playerTrans, Vector2 movementInput) {
         Vector2 playerDir = new Vector2(playerTrans.forward.x, playerTrans.forward.z);
         float angleDiff = Vector2.SignedAngle(Vector2.up, playerDir);
         return Rotate(movementInput, -angleDiff);
     }
 
     private Vector2 Rotate(Vector2 v, float degrees) {
         float radians = degrees * Mathf.Deg2Rad;
         float sin = Mathf.Sin(radians);
         float cos = Mathf.Cos(radians);
 
         float tx = v.x;
         float ty = v.y;
 
         return new Vector2(cos * tx - sin * ty, sin * tx + cos * ty);
     }
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 Fulcrum_Games · Feb 24 at 03:49 PM 1
Share

Thank you so much for this! I knew the solution was something along these lines but I hammered it out on paper for a couple hours and couldn't get the right conversion. This idea works perfectly.

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

193 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

Related Questions

Character Movement w/ Both Joysticks 0 Answers

2D Topdown Character Contoller 0 Answers

Face target first then move forward to it - Top Down 2D 1 Answer

input mouseposition jitter 1 Answer

Slow Character Movement 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