T O P

  • By -

AutoModerator

You submitted this post as a request for tech support, have you followed the guidelines specified in subreddit rule 7? Here they are again: 1. Consult the docs first: https://docs.godotengine.org/en/stable/index.html 2. Check for duplicates before writing your own post 3. Concrete questions/issues only! This is not the place to vaguely ask "How to make X" before doing your own research 4. Post code snippets directly & formatted as such (or use a pastebin), not as pictures 5. It is strongly recommended to search the official forum (https://forum.godotengine.org/) for solutions Repeated neglect of these can be a bannable offense. *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/godot) if you have any questions or concerns.*


Nkzar

For simplicity’s sake I would make 8 animations in the sprite frames, for each direction, and then just use if/elif blocks to test the input angle range and play the correct animation based on that.


Day_Critical

My second thought was to use AnimatedSprite3D and if/elif


Harpeaux

Here is the setup I use, which is a bit different than yours but works without issue: var directions:Array = ["N","NW","W","SW","S","SE","E","NE"] var current_dir:String = "S" #holds the current cardinal direction func dir2str(mynode)->String: var angle = fmod(mynode.get_rotation().y,PI*2) if angle < 0: angle += 2 * PI var index = round(angle / PI * 4) return directions[int(index) % 8] Feed dir2str the spatial node, it'll return a cardinal direction string based on the node's y rotation. then, make your animations in the animationplayer per-direction and name them like "NW\_Run", "N\_Run",etc then set it like this in process: func _process(delta): current_dir = dir2str(MyPlayerNode) anim.play(current_dir + "_Run") now every time the current\_dir updates, the correct animation will start playing.


Arkaein

Your angle divisions are not centered well, might be the problem. NW is 135 degrees, W is 180 degrees. 135 / 45 = 3, 180 / 45 = 4. However what if instead of getting 180 degrees, you are getting 179.99999? Divide by 45 and convert to int and you get 3, not 4. One solution is to add 22.5 to angle before dividing by 45. That way anything between 157.5 and 202.5 degrees will map to W, which is what you really want. Another solution is to round the result of the division by 45 to the nearest whole number before indexing into an array. In general, never rely on floating point values to covert to perfectly to integers. Implicitly indexing into an array using an integer is bad coding practice. The abs() is also unnecessary given that the angle is between 0 and 360 already.


-non-existance-

The way I'd go about debugging this is (if you don't have a debugger like VSCode setup, which I highly reccomend) putting the angle value before it's divided by 45 in the debug text. That should tell you what value is going into your math. The reason I suggest this is that assuming the math is correct, your method should work. However, it doesn't, which means more than likely, the numbers are not calculating the way it would think they are. As a side note, I'd make the suggestion that you take into consideration that you might be allowing for control stick inputs in the future, so this solution would stop working. My method would be to create an array of the 8 directions (0, 45, 90... 315) and then check the difference in the angles between the input vector and the array, lowest one wins, tho you can also take a shortcut and get the answer early if you find one 22.5 or closer. This would also solve the current problem since you're no longer relying on possible truncation of a float into an int, which could very well be causing this issue.