• 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 /
This question was closed Oct 26, 2020 at 10:19 AM by Ardaze for the following reason:

Other

avatar image
0
Question by Ardaze · Jan 03, 2020 at 08:45 AM · 2dmovementpathfindingisometric

2D Isometric Movement Path (8 Directions)

Hi there,

I'm developing a 2d isometric game and the character must move in 8 directions, i have created animations but the character can't find the right animation while he's going to the target!

 public void AngleToVectorDirection(Vector2 dir)
     {
         Vector2 normDir = dir.normalized;
         //calculate how many degrees one slice is
         float step = 360f / 8;
         //calculate how many degress half a slice is.
         //we need this to offset the pie, so that the North (UP) slice is aligned in the center
         float halfstep = step / 2;
         //get the angle from -180 to 180 of the direction vector relative to the Up vector.
         //this will return the angle between dir and North.
         //
         float angle = Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg;
 
         float x = dir.x;
         float y = dir.y;
 
         if (angle >= 157.5 && angle < 202.5)
         {
             animator.Play("RunRight");
             return;
         }
         else if (angle >= 112.5 && angle < 157.5)
         {
             animator.Play("RunUpRight");
             return;
         }
         else if (angle >= 67.5 && angle < 112.5)
         {
             animator.Play("RunUp");
             return;
         }
         else if (angle >= 22.5 && angle < 67.5)
         {
             animator.Play("RunUpLeft");
             return;
         }
         else if (angle >= 337.5 || angle < 22.5)
         {
             animator.Play("RunLeft");
             return;
         }
         else if (angle >= 292.5 && angle < 337.5)
         {
             animator.Play("RunDownLeft");
             return;
         }
         else if (angle >= 247.5 && angle < 292.5)
         {
             animator.Play("RunDown");
             return;
         }
         else if (angle >= 202.5 && angle < 247.5)
         {
             animator.Play("RunDownRight");
             return;
         }
     }
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

  • Sort: 
avatar image
1

Answer by Bunny83 · Jan 03, 2020 at 10:34 AM

Is it possible that you just copy and pasted all that code together somehow? Because have you actually read the comment before the "Atan2" line? "angle" will be in the range -180 to 180. So many of your if statements can never be reached since you have values greater than 180 as minimum condition. You probably wanted to "shift" the angle from the range [-180, 180] to [0, 360]. To do that you have to add 180 to the angle.


Note you want to add an f suffix to all your number constants in your if statements. Otherwise you force an implicit type conversion of angle from float to double in every if statement. I'm also wondering since you calculated your "halfstep" (22.5), why have you used hardcoded constants?


Currently you have 8 if statements. In the worst case you have to check all 8 every frame. It's usually simpler to just work with the normalized direction vector and doing a 4 step nested if - else chain. That way the overhead stays constant and you don't need to calculate atan2. However you can of course stick to your angle based approach. Though currently you check every limit twice which is not necessary in an else-if chain. All you have to do is order your statements correctly. You also do not need the return statements since an else-if chain is broken automatically once it enters one statement.

 float angle = 180f + Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg;
 
 if (angle > 337.5f || angle < 22.5f)
 {
     animator.Play("RunLeft");
 }
 else if (angle > 292.5f)
 {
     animator.Play("RunDownLeft");
 }
 else if (angle > 247.5f)
 {
     animator.Play("RunDown");
 }
 else if (angle > 202.5f)
 {
     animator.Play("RunDownRight");
 }
 if (angle > 157.5f)
 {
     animator.Play("RunRight");
 }
 else if (angle > 112.5f)
 {
     animator.Play("RunUpRight");
 }
 else if (angle > 67.5f)
 {
     animator.Play("RunUp");
 }
 else
 {
     animator.Play("RunUpLeft");
 }



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
avatar image
0

Answer by Ardaze · Jan 03, 2020 at 12:43 PM

This is how i set animations and values.

Check the Image: https://ibb.co/8MY9G4k

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

282 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 Pathfinding Top Down 0 Answers

Move towards move very fast independent of the speed 1 Answer

How to highlight only walkable path on tilemap,Sorting walkable path on a tilemap 0 Answers

2D C# isometric Tile-based movement and collision problem 1 Answer

vertical and horizontal movement not working at the same time 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