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 jfonz001 · Nov 09, 2017 at 12:56 AM · 2ddirectionanglesdegreespoint-and-click

Play appropriate animation based on character's direction (2D point and click)

I have a simple script to control player movement:

 void MovePlayer(){
     if (Input.GetMouseButtonDown(0)) {
         target = (Camera.main.ScreenToWorldPoint(Input.mousePosition));
         target.z = transform.position.z; 
     }
     transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);

     PlayWalkingAnimation();
 }

This enabled me to simply click my mouse anywhere on the screen, and the Player will move to that point.

I have four walking animations (NWSE) that i'd like to play depending on the angle the Player is walking in...if that makes sense...

 void PlayWalkingAnimation(movementAngle){

     //What is movementAngle???

     if (movementAngle >= 45 degrees && movementAngle < 135 degrees){ 
         direction  = "walking north";
         anim.Play (direction);
     } else if (movementAngle >= 135 degrees && movementAngle < 225 degrees)){ 
         direction  = "walking west";
         anim.Play (direction);
     } else if (movementAngle >= 225 degrees && movementAngle < 315 degrees){ 
         direction = "walk south";
         anim.Play (direction);
     } else {
         direction = "walking east"; 
         anim.Play (direction);
     }
 }

I'm not sure how to get the angle I'm moving in, and if there is any math that might be involved.

Also my if-statements might not be spot on, but this is the general idea of what I'm trying to do...

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 Daemonhahn · Nov 09, 2017 at 11:11 AM

You should use a blendtree, that way you wont even need to write code to get this to work!

For more info:

https://docs.unity3d.com/Manual/class-BlendTree.html

and

https://www.youtube.com/watch?v=-UhAAgVjk0A

Good luck! :)

Essentially you make a 2D blend tree, with a point at the top bottom left and right, and one in the centre for your idle animation. You then assign the animations to each of the slots, and give a -1 +1 in the X / Y field respectively to say "this animation is for when we are moving left on the X axis" etc.

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 Daemonhahn · Nov 09, 2017 at 12:11 PM 0
Share

Please select my answer as the correct one if I have helped you! :)

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

138 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

Related Questions

Clamp rotation of sprite? 0 Answers

Instantiate after setting values? 0 Answers

Camera rotating super fast when trying to match the camera and players Y euler angle. 1 Answer

Using direction and Rigidbody2D.AddForce() to move towards object. 1 Answer

Rotate an arrow object based on addedForce 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