• Best Answerset by NJL64

NJL64 Your logic is flawed here. If the thing bounces perceptually (but not perfectly) in the cardinal direction both velocity components will be non-zero although one of them may be very small, but since you only check for a perfect cardinal bounce all non perfect ones will be registered as "diagonal".

    • Edited

    xyz I understand. That makes perfect sense. I think that's what's wrong actually. Thank you!

    Is there a way I could possibly check for directions better, so that I can check for imperfect cardinal bounces?

    I know my homemade get_direction() function isn't the best and probably only accounts for 8 perfect directions.

    • xyz replied to this.

      xyz Thank you so much! I'm gonna try that.

      • Edited

      xyz Sorry. I know you said you answered similar questions already, but I was wondering if you could help me understand a way to apply this to my function?

      Mine's not for inputs, but rather just the character moving in general.

      I just thought I'd ask ahead of time in case I have trouble. I'm researching some of this stuff right now and thinking of how to apply it to my script.

      When I get the angle of the velocity vector and snap it to 45 degrees, what should I do with it then? (I'm still brushing up on my Vector math)

      I would really appreciate it. 🙂

      • xyz replied to this.

        NJL64
        Just use your velocity vector instead of input direction vector. You can disregard the tolerance stuff as it doesn't apply to your case

          • Edited

          xyz So I wrote this function (although I admit I don't understand it myself):

          func get_dir():
          	var dir = velocity
          	var angle = posmod(rad_to_deg(dir.angle()), 360)
          	var sector = wrapi(snapped(angle, 45) / 45, 0, 8)

          But, how should I use it to update the value of "dir_last"? 🙁

          • xyz replied to this.
            • Edited

            NJL64 Return the value from the function and assign it to dir_last

              xyz What's the value from the function?

              • xyz replied to this.
                • Edited

                NJL64 Functions can return values. That's in fact one of their main purpose, to calculate something and then return it. Look at my code on the link again. The function there returns a direction string.

                  xyz So I tried this:

                  func get_dir():
                  	var dir = velocity
                  	var angle = posmod(rad_to_deg(dir.angle()), 360)
                  	var sector = wrapi(snapped(angle, 45) / 45, 0, 8)
                  	return ["E", "SE", "S", "SW", "W", "NW", "N", "NE"] [sector]

                  and this:

                  dir_last = get_dir()

                  and it works for input and seems to work for bouncing off of things! But if I could fix one last issue, it should work how I want it.

                  Unfortunately I can no longer say

                  velocity = dir_last * speed

                  when shooting my character in the direction facing using the "BLAST" state.

                  If you would be so, SO generous to help me with with an alternative so that I can still shoot the character in the direction they are facing, it would really help me out.

                  Thank you!

                  • xyz replied to this.

                    NJL64 Return direction vectors instead of strings.