In _physics_process, you could accumulate the values of velocity
and dir_last
in arrays. For example, store 100 values before and after a bounce. Then you could print and look at them after a bounce, to see what happened.
Bug help?: Last direction gets updated incorrectly when object bounces
- 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.
- Edited
NJL64 Get the angle of the velocity vector and snap it to 45 deg increments.
I think I answered very similar question(s) couple of times already in the past
Take a look here:
https://godotforums.org/d/38606-8-directional-input/2
- 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 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 @DaveTheCoder Thank you!