Guest User

Player velocity bug

a guest
Aug 22nd, 2019
76
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends '../Movement.gd'
  2.  
  3. export(float) var BASE_MAX_HORIZONTAL_SPEED = 400.0
  4.  
  5. export(float) var AIR_ACCELERATION = 1000.0
  6. export(float) var AIR_DECCELERATION = 2000.0
  7. export(float) var AIR_STEERING_POWER = 50.0
  8.  
  9. export(float) var GRAVITY = 1600.0
  10.  
  11. var enter_velocity = 0.0
  12.  
  13. var max_horizontal_speed = 0.0
  14. var horizontal_speed = 0.0
  15. var horizontal_velocity = 0.0
  16.  
  17.  
  18. func initialize(speed, velocity):
  19. horizontal_speed = speed
  20. max_horizontal_speed = speed if speed > 0.0 else BASE_MAX_HORIZONTAL_SPEED
  21. enter_velocity = velocity
  22.  
  23.  
  24. func enter():
  25. var input_direction = get_input_direction()
  26. #get_input_direction get's the input direction normal.
  27. #For exmple, if A and D are pressed, it returns 0. If D is pressed, 1, and vice-versa
  28.  
  29. if owner.is_on_floor():
  30. emit_signal("finished", "previous")
  31.  
  32. func update(delta):
  33. var input_direction = get_input_direction()
  34. update_look_direction(input_direction)
  35.  
  36. fall(delta, input_direction)
  37. print(owner.is_on_floor())
  38. if owner.is_on_floor():
  39. emit_signal("finished", "previous")
  40.  
  41. ### VVV THIS FUNCTION MOST LIKELY PRODUCES THE BUG VVV
  42.  
  43. func fall(delta, direction):
  44. if Input.is_action_pressed("snapshot"):
  45. return
  46. if direction:
  47. horizontal_speed += AIR_ACCELERATION * delta
  48. else:
  49. horizontal_speed -= AIR_DECCELERATION * delta
  50. horizontal_speed = clamp(horizontal_speed, 0, max_horizontal_speed)
  51.  
  52. var target_velocity = horizontal_speed * direction
  53. var steering_velocity = (target_velocity - horizontal_velocity) * AIR_STEERING_POWER
  54. horizontal_velocity += steering_velocity
  55.  
  56. owner.move_and_slide(Vector2(horizontal_velocity, 0))
  57. #I'm hoping to incorporate vertical velocity or gravity in this Vector2 at some point
RAW Paste Data