Advertisement
Guest User

Untitled

a guest
Nov 12th, 2023
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 8.50 KB | Source Code | 0 0
  1. extends CharacterBody3D
  2.  
  3. const WALK_SPEED: float = 700.0
  4. const RUN_SPEED: float = 1500.0
  5. const JUMP_HEIGHT: float = 12
  6. const SLIDE_LENGTH: float = 7.0
  7. var is_jump_performed: bool = false
  8. var is_jump_buffered: bool = false
  9. var is_jump_released: bool = false
  10. var is_jump_first_time_released: bool = false
  11. var can_jump: bool = false
  12. var speed: float = 0.0
  13. var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
  14.  
  15. var dir_when_enter_air: Vector3 = Vector3.ZERO
  16. var jump_is_straight: bool = false
  17. var jump_is_dir: bool = false
  18. var is_air_move_canceled: bool = false
  19. var is_air_strafing_on_z: bool = false
  20. var is_air_strafing_on_x: bool = false
  21. var did_diag_air_strafe: bool = false
  22. var can_diag_air_strafe: bool = true
  23. var still_diag_air_strafing: bool = false
  24. var is_diag_air_canceled: bool = false
  25. const NORMAL_AIR_MULTIPLIER: float = 1.1
  26. const STRAFE_AIR_MULTIPLIER: float = 0.25
  27. const CANCELED_AIR_MULTIPLIER: float = -0.75
  28. const RELEASE_CANCELED_AIR_MULTIPLIER: float = -0.37
  29. var air_multiplier: float = 0.0
  30. var is_running: bool = false
  31. var is_run_active: bool = false
  32. var is_run_buffered: bool = false
  33. var is_run_jump_release_buffered: bool = false
  34.  
  35. var is_land_slide_canceled: bool = false
  36. var just_landed: bool = false
  37. var is_on_land_already: bool = false
  38. var dir_when_landing: Vector3 = Vector3.ZERO
  39. var friction: float = 1
  40.  
  41. var y_dir: float = 0
  42. var input_dir: Vector3 = Vector3.ZERO
  43. var move_dir: Vector3 = Vector3.ZERO
  44. var air_move_dir: Vector3 = Vector3.ZERO
  45. var move_x: float = 0.0
  46. var move_z: float = 0.0
  47.  
  48. var dead: bool = false
  49.  
  50. @onready var move_dir_pivot = $MovementDirectionPivot
  51.  
  52. func _unhandled_input(event: InputEvent)-> void:
  53.    
  54.     if dead == false:
  55.         if event is InputEventMouseButton:
  56.             Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
  57.            
  58.         elif event.is_action_pressed("esc"):
  59.             Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
  60.        
  61.         if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
  62.             if event is InputEventMouseMotion:
  63.                 move_dir_pivot.rotate_y(-event.relative.x * 0.01)
  64.  
  65. func _physics_process(delta):
  66.     if is_on_floor() == false:
  67.         y_dir -= gravity * 3 * delta
  68.     else:
  69.         dir_when_enter_air = Vector3.ZERO
  70.         is_run_active = false
  71.         y_dir = 0
  72.         is_jump_first_time_released = false
  73.         air_multiplier = NORMAL_AIR_MULTIPLIER
  74.         is_air_strafing_on_z = false
  75.         is_air_strafing_on_x = false
  76.         is_run_jump_release_buffered = false
  77.         did_diag_air_strafe = false
  78.         can_diag_air_strafe = true
  79.         is_jump_performed = false
  80.         jump_is_straight = false
  81.         jump_is_dir = false
  82.    
  83.     if dead == false:
  84.        
  85.         jump()
  86.         move()
  87.        
  88.         if is_on_floor() == false:
  89.            
  90.             just_landed = true
  91.             is_on_land_already = false
  92.             friction = 1
  93.            
  94.             move_dir = air_move_dir * speed * delta
  95.             velocity = move_dir_pivot.basis * move_dir
  96.             velocity.y = y_dir
  97.             move_and_slide()
  98.            
  99.             air_strafe_calc()
  100.             air_cancel_calc()
  101.        
  102.         else:
  103.            
  104.             if is_on_land_already == false and just_landed == true \
  105.             or friction > 0.0 and friction < 1.0:
  106.                 if is_land_slide_canceled == false and is_jump_performed == true:
  107.                     can_jump = false
  108.                     velocity = lerp(move_dir_pivot.basis * dir_when_landing * speed * delta, \
  109.                     move_dir_pivot.basis * Vector3(dir_when_landing.x * speed, y_dir, dir_when_landing.z * speed) * 1.2 * delta, friction)
  110.                     velocity.y = y_dir
  111.                     move_and_slide()
  112.                     is_on_land_already = true
  113.                     just_landed = false
  114.                 elif is_land_slide_canceled == true and is_jump_performed == true:
  115.                     velocity = Vector3.ZERO
  116.                     velocity.y = y_dir
  117.                     move_and_slide()
  118.            
  119.             if friction <= 0.0:
  120.                 friction = 0
  121.                 is_land_slide_canceled = false
  122.             else:
  123.                 friction -= 0.2
  124.            
  125.             if friction <= 0.8:
  126.                 can_jump = true
  127.            
  128.             move_dir = input_dir * speed * delta
  129.             velocity = move_dir_pivot.basis * move_dir
  130.             velocity.y = y_dir
  131.             move_and_slide()
  132.  
  133. func move():
  134.     move_x = Input.get_axis("move_left", "move_right")
  135.     move_z = Input.get_axis("move_forwards", "move_backwards")
  136.     input_dir = Vector3(move_x, 0, move_z)
  137.     if input_dir != null:
  138.         input_dir.normalized()
  139.    
  140.     if Input.is_action_pressed("run") and is_jump_first_time_released == false \
  141.     or is_run_active == true and is_jump_first_time_released == false:
  142.         is_running = true
  143.         is_run_buffered = true
  144.         speed = RUN_SPEED
  145.    
  146.     elif is_run_active == true and is_jump_first_time_released == true \
  147.     and is_on_floor() == false or is_run_jump_release_buffered == true:
  148.         is_running = true
  149.         is_run_jump_release_buffered = true
  150.         speed = RUN_SPEED * 1.1
  151.    
  152.     elif Input.is_action_pressed("run") == false and is_run_active == false:
  153.         is_running = false
  154.         speed = WALK_SPEED
  155.         is_run_buffered = false
  156.  
  157. func jump():
  158.    
  159.     if Input.is_action_just_pressed("jump") and is_on_floor() == true and can_jump == true \
  160.     and is_jump_performed == false or is_jump_buffered == true and is_on_floor() == true:
  161.         is_jump_performed = true
  162.         is_run_active = is_run_buffered
  163.         dir_when_enter_air = input_dir
  164.         dir_when_landing = dir_when_enter_air
  165.         is_jump_buffered = false
  166.         is_jump_released = false
  167.         y_dir += JUMP_HEIGHT
  168.        
  169.     if Input.is_action_just_released("jump") and is_jump_performed == true \
  170.     and is_jump_released == false and is_on_floor() == false:
  171.         is_jump_released = true
  172.         is_jump_first_time_released = true
  173.    
  174.     elif Input.is_action_just_released("jump"):
  175.         is_jump_buffered = false
  176.        
  177.     if Input.is_action_pressed("jump") and is_jump_performed == true \
  178.     and is_jump_released == true and is_jump_buffered == false \
  179.     and is_on_floor() == false:
  180.         is_jump_buffered = true
  181.  
  182. func air_cancel_calc():
  183.     if is_jump_performed == true:
  184.         if dir_when_enter_air == Vector3(0, 0, 0):
  185.             jump_is_straight = true
  186.        
  187.         if jump_is_straight == false:
  188.             if dir_when_enter_air.x != input_dir.x and input_dir.z == 0 \
  189.             or dir_when_enter_air.z != input_dir.z and input_dir.x == 0:
  190.                 air_move_dir = dir_when_enter_air * 0.4
  191.                 if input_dir != Vector3(0, y_dir, 0):
  192.                     is_land_slide_canceled = true
  193.                     friction = 0.0
  194.                 is_air_move_canceled = true
  195.            
  196.             elif did_diag_air_strafe == true and dir_when_enter_air != input_dir:
  197.                 if input_dir.x == dir_when_enter_air.x and input_dir.z != dir_when_enter_air.z \
  198.                 and input_dir.z != -dir_when_enter_air.z and input_dir.x != -dir_when_enter_air.x \
  199.                 or input_dir.z == dir_when_enter_air.z and input_dir.x != dir_when_enter_air.x \
  200.                 and input_dir.z != -dir_when_enter_air.z and input_dir.x != -dir_when_enter_air.x:
  201.                     still_diag_air_strafing = true
  202.                 else:
  203.                     still_diag_air_strafing = false
  204.                     is_diag_air_canceled = true
  205.                     is_land_slide_canceled = true
  206.                    
  207.             elif dir_when_enter_air.z == input_dir.z \
  208.             and dir_when_enter_air.x != input_dir.x and input_dir != Vector3.ZERO \
  209.             and is_air_move_canceled == false:
  210.                 is_air_strafing_on_z = true
  211.            
  212.             elif dir_when_enter_air.x == input_dir.x \
  213.             and dir_when_enter_air.z != input_dir.z and input_dir != Vector3.ZERO \
  214.             and is_air_move_canceled == false:
  215.                 is_air_strafing_on_x = true
  216.         elif jump_is_straight == true and jump_is_dir == false:
  217.             air_multiplier = STRAFE_AIR_MULTIPLIER
  218.  
  219. func air_strafe_calc():
  220.     if is_jump_performed == true:
  221.         if is_air_strafing_on_z == true and did_diag_air_strafe == false \
  222.         and still_diag_air_strafing == false:
  223.             can_diag_air_strafe = false
  224.             air_move_dir = Vector3(input_dir.x * STRAFE_AIR_MULTIPLIER, y_dir, input_dir.z * air_multiplier)
  225.        
  226.         elif is_air_strafing_on_x == true and did_diag_air_strafe == false \
  227.         and still_diag_air_strafing == false:
  228.             can_diag_air_strafe = false
  229.             air_move_dir = Vector3(input_dir.x * air_multiplier, y_dir, input_dir.z * STRAFE_AIR_MULTIPLIER)
  230.        
  231.         elif is_air_strafing_on_z == false and move_x == 0 \
  232.         and did_diag_air_strafe == false and is_diag_air_canceled == false:
  233.             can_diag_air_strafe = false
  234.             air_move_dir = Vector3(0, y_dir, input_dir.z * air_multiplier)
  235.        
  236.         elif is_air_strafing_on_x == false and move_z == 0 \
  237.         and did_diag_air_strafe == false and is_diag_air_canceled == false:
  238.             can_diag_air_strafe = false
  239.             air_move_dir = Vector3(input_dir.x * air_multiplier, y_dir, 0)
  240.            
  241.         elif is_diag_air_canceled == true and did_diag_air_strafe == true \
  242.         and still_diag_air_strafing == false:
  243.             can_diag_air_strafe = false
  244.             air_move_dir = Vector3(0, y_dir, 0)
  245.                
  246.         else:
  247.             if can_diag_air_strafe == true or still_diag_air_strafing == true:
  248.                 did_diag_air_strafe = true
  249.                 still_diag_air_strafing = true
  250.                 air_move_dir = Vector3(dir_when_enter_air.x * air_multiplier, y_dir, dir_when_enter_air.z * air_multiplier)
  251.     else:
  252.         air_move_dir = input_dir * air_multiplier
  253.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement