• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By MadeWithRealLemons
      Hello all, I'm pretty new to programming and game development but I'm currently studying computer science at FSU and am just working on a project on that side that I hope to put on my resume one day... Any and all advice would be welcome!  I made a quick, shitty, little dev vlog where I just spliced a few of the features I've added into a single video.
      Please leave a comment if you have any words of encouragement or advice!  
    • By abarnes
      Hello All!
      I am currently pursuing a degree in video game programming, so far I have completed an intro to programming course and object oriented programming course. Both were taught using C++ as the programming langauge which I know is very popular for game development, but in these classes we do not actually do any game development. I would like to start to build my skills with C++ for game development as that is a common required thing for a job and am looking for ways to do this. Any recommendations such as books to read or youtube videos to watch will be greatly appreciated!
    • By RoKabium Games
      While looking out for that pesky Terrator, our little alien is doing a bit of relaxed mining down on the new gas planet "Lelantos" this weekend.... 
      #gamedev #indiedev #madewithunity #screenshotsaturday
    • By vividgamer
      I have a native iOS game (objective c, XCode build) which I am considering to port to other platforms.
      Core gameplay is based on solely on geographical maps, and custom drawing over maps. It also has Core Data. This part is complete in development.
      What is not done yet is: monetization, gamification (leaderboards, challenges) and multiplayer functionality.
      As I think more about it, I am tempted to think if this is the right time to move to a cross platform tool such as Unity. But before dedicating time to port my 5 years side-project effort in Objective C, I really want to know if its worth it.
      - Does Unity support such plugins / assets that will fulfill all my above requirements?
      - Unity Personal seems to have only 20 concurrent users - is it too costly scaling if I decide for extending to web and android platforms?
      - What is the general workflow involved in publishing to iOS, Android, PC, and web platforms while using Unity? I mean to ask about various points of signing stuff, paying fees and getting certified.
      - How long will it really take to port my entire Objective C project into Unity? I am somewhat familiar with C# but I am finding it hard fidgeting with Unity IDE as lot of things are focused around FPS and 3D while my game is still 2d - not much action involved. I seem bit overwhelmed by the list of features I see there. All in all, I do not want to lose my momentum while still making sure its portable to everywhere.
      - Any assets I could use (for free to try basis in debug) that are relevant for my game?
      - Last but not the least, are there any costs that I need to be paying upfront to Unity, for using it (apart from their monthly subscription model)? I don't understand their costing for multiplayer in conjunction with their subscription fees - if someone could kindly elaborate.
      Thanks in advance for your time reading a newbie
  • Advertisement
  • Advertisement

Walking-Animation based on angle between character and destination

Recommended Posts

Hello fellow devs!

Once again I started working on an 2D adventure game and right now I'm doing the character-movement/animation. I'm not a big math guy and I was happy about my solution, but soon I realized that it's flawed.

My player has 5 walking-animations, mirrored for the left side: up, upright, right, downright, down. With the atan2 function I get the angle between player and destination. To get an index from 0 to 4, I divide PI by 5 and see how many times it goes into the player-destination angle.

In Pseudo-Code:
angle = atan2(destination.x - player.x, destination.y - player.y) //swapped y and x to get mirrored angle around the y axis
index = (int) (angle / (PI / 5));
PlayAnimation(index); //0 = up, 1 = up_right, 2 = right, 3 = down_right, 4 = down

Besides the fact that when angle is equal to PI it produces an index of 5, this works like a charm. Or at least I thought so at first. When I tested it, I realized that the up and down animation is playing more often than the others, which is pretty logical, since they have double the angle.

What I'm trying to achieve is something like this, but with equal angles, so that up and down has the same range as all other directions.

I can't get my head around it. Any suggestions? Is the whole approach doomed?

Thank you in advance for any input! :)

Edited by swiftcoder
Removed [solved]

Share this post

Link to post
Share on other sites

Normally when dealing with animations like this just use an array to hold the sprites. That way your first line of code would have work with 2PI and /8.

The array looks like this [0: Up45 , 1: right, 2: Down45, 3: Down ,4: Down45,5: right,6: Up45,7: UP]. The last 4,5,6 will be mirrored.

Then it's just:

int Index = (Angle / (Circle/8)) -1;                     // (2PI)/8 or 360/8

What happens is that Circle/ 8  = 45 degrees. So if your half way (180 / 45) = 4 -1 = 3 the down sprite. 360/45 = 8 -1 = 7 the up sprite.

The nice thing here is that it's easy to understand and works with both PI and Degrees. The other nice thing is that if you wanted to use it for aiming at the mouse you can just use an array of 16.


Or just - 22.5 //((circle/amount)/2);

Vectors can also be used here as it has all the the info packed inside. In fact when working with an axis input like Up and Down vectors are often smoother.

Share this post

Link to post
Share on other sites

Thank you very much Scouting Ninja! Great solution, I did't think about that :) I also thought about doing it with vectors, but I didn't know how, so this was the best I could come up with xD Again, thanks for a quick and elaborate reply.

@swiftcoder Makes sense! I won't do it again :)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement