Advertisement
Guest User

Godot character 2D

a guest
Jul 10th, 2017
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1.  
  2. extends KinematicBody2D
  3.  
  4. const GRAVITY = 500.0 # Pixels/second
  5.  
  6. # Angle in degrees towards either side that the player can consider "floor"
  7. const FLOOR_ANGLE_TOLERANCE = 40
  8. const WALK_FORCE = 600
  9. const WALK_MIN_SPEED = 10
  10. const WALK_MAX_SPEED = 200
  11. const STOP_FORCE = 1300
  12. const JUMP_SPEED = 200
  13. const JUMP_MAX_AIRBORNE_TIME = 0.2
  14.  
  15. const SLIDE_STOP_VELOCITY = 1.0 # One pixel per second
  16. const SLIDE_STOP_MIN_TRAVEL = 1.0 # One pixel
  17.  
  18. var velocity = Vector2()
  19. var on_air_time = 100
  20. var jumping = false
  21.  
  22. var prev_jump_pressed = false
  23.  
  24.  
  25. func _fixed_process(delta):
  26. # Create forces
  27. var force = Vector2(0, GRAVITY)
  28.  
  29. var walk_left = Input.is_action_pressed("move_left")
  30. var walk_right = Input.is_action_pressed("move_right")
  31. var jump = Input.is_action_pressed("jump")
  32.  
  33. var stop = true
  34.  
  35. if (walk_left):
  36. if (velocity.x <= WALK_MIN_SPEED and velocity.x > -WALK_MAX_SPEED):
  37. force.x -= WALK_FORCE
  38. stop = false
  39. elif (walk_right):
  40. if (velocity.x >= -WALK_MIN_SPEED and velocity.x < WALK_MAX_SPEED):
  41. force.x += WALK_FORCE
  42. stop = false
  43.  
  44. if (stop):
  45. var vsign = sign(velocity.x)
  46. var vlen = abs(velocity.x)
  47.  
  48. vlen -= STOP_FORCE*delta
  49. if (vlen < 0):
  50. vlen = 0
  51.  
  52. velocity.x = vlen*vsign
  53.  
  54. # Integrate forces to velocity
  55. velocity += force*delta
  56.  
  57. # Integrate velocity into motion and move
  58. var motion = velocity*delta
  59.  
  60. # Move and consume motion
  61. motion = move(motion)
  62.  
  63. var floor_velocity = Vector2()
  64.  
  65. if (is_colliding()):
  66. # You can check which tile was collision against with this
  67. # print(get_collider_metadata())
  68.  
  69. # Ran against something, is it the floor? Get normal
  70. var n = get_collision_normal()
  71.  
  72. if (rad2deg(acos(n.dot(Vector2(0, -1)))) < FLOOR_ANGLE_TOLERANCE):
  73. # If angle to the "up" vectors is < angle tolerance
  74. # char is on floor
  75. on_air_time = 0
  76. floor_velocity = get_collider_velocity()
  77.  
  78. if (on_air_time == 0 and force.x == 0 and get_travel().length() < SLIDE_STOP_MIN_TRAVEL and abs(velocity.x) < SLIDE_STOP_VELOCITY and get_collider_velocity() == Vector2()):
  79. # Since this formula will always slide the character around,
  80. # a special case must be considered to to stop it from moving
  81. # if standing on an inclined floor. Conditions are:
  82. # 1) Standing on floor (on_air_time == 0)
  83. # 2) Did not move more than one pixel (get_travel().length() < SLIDE_STOP_MIN_TRAVEL)
  84. # 3) Not moving horizontally (abs(velocity.x) < SLIDE_STOP_VELOCITY)
  85. # 4) Collider is not moving
  86.  
  87. revert_motion()
  88. velocity.y = 0.0
  89. else:
  90. # For every other case of motion, our motion was interrupted.
  91. # Try to complete the motion by "sliding" by the normal
  92. motion = n.slide(motion)
  93. velocity = n.slide(velocity)
  94. # Then move again
  95. move(motion)
  96.  
  97. if (floor_velocity != Vector2()):
  98. # If floor moves, move with floor
  99. move(floor_velocity*delta)
  100.  
  101. if (jumping and velocity.y > 0):
  102. # If falling, no longer jumping
  103. jumping = false
  104.  
  105. if (on_air_time < JUMP_MAX_AIRBORNE_TIME and jump and not prev_jump_pressed and not jumping):
  106. # Jump must also be allowed to happen if the character left the floor a little bit ago.
  107. # Makes controls more snappy.
  108. velocity.y = -JUMP_SPEED
  109. jumping = true
  110.  
  111. on_air_time += delta
  112. prev_jump_pressed = jump
  113.  
  114.  
  115. func _ready():
  116. set_fixed_process(true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement