NoneApplicableGames

player_character_controller.gd

Apr 19th, 2025
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 2.96 KB | Source Code | 0 0
  1. extends CharacterBody3D
  2.  
  3. class_name PlayerCharacterController
  4.  
  5.  
  6. ##The script to handle the character's movement.
  7. ##It governs everything in relation on player physics and input handling.
  8.  
  9.  
  10. ##Export properties for anything we need to tune the value for.
  11. ##We want to avoid hardcoding as much as possible
  12.  
  13. @export_category("Ground Movement Properties")
  14. @export var max_speed : float ## Max speed the player can achive horrizontally.
  15. @export var acceleration : float ## The force applied to the player as they accelerate along the ground.
  16. @export var friction : float ## The force applied to the player as they come to a stop.
  17.  
  18. @export_category("Air Movement Properties")
  19. @export_range(1.0, 500.0, 0.1) var jump_force : float = 10.0 ## The force applied to the player when they initally jump.
  20. @export_range(1.0, 100.0, 0.1) var down_force : float = 1.0 ## A downwards force applied to the player's jump as they are rising to create a proper parabolic arc
  21. @export_range(1.0, 500.0, 0.1) var gravity := 17.0 ##constant force of gravity applied to falling player
  22. @export_range(1.0, 1000.0, 0.1) var terminal_velocity := 20.0 ##player's max falling speed
  23. var _intial_jump_force : float ##proxy varible to store jump force as it decreases over the course of the arc. Allows it to be reset later.
  24. var _jumping : bool = false
  25.  
  26. func _ready() -> void:
  27.     _intial_jump_force = jump_force
  28.  
  29. func _physics_process(delta: float) -> void:
  30.     ###Player movement physics
  31.    
  32.     ##How the player moves on the ground
  33.     var move_input_vector : Vector2 = Input.get_vector("move_left", "move_right", "move_up", "move_down")
  34.    
  35.    
  36.     #input_vector is a vector2, we will need to conver to vector3
  37.    
  38.     var move_direction := Vector3 (move_input_vector.x, 0.0, move_input_vector.y)
  39.    
  40.     #check if input vector is ZERO to reduce unesscary errors
  41.     if move_direction != Vector3.ZERO:
  42.         var horrizontal_velocity := move_direction * max_speed
  43.         self.velocity = horrizontal_velocity
  44.    
  45.     else:
  46.         self.velocity = Vector3.ZERO
  47.    
  48.    
  49.    
  50.     if Input.is_action_just_pressed("jump") && is_on_floor():
  51.        
  52.         #to create a smooth parabola we want to decrease the amount of velocity evey physics tick.
  53.        
  54.         var jump : Vector3= jump_force * self.up_direction * delta
  55.        
  56.         self.velocity.y += jump_force
  57.        
  58.         _jumping = true
  59.        
  60.    
  61.     ##Handles everything to do with falling
  62.    
  63.     if !is_on_floor():
  64.        
  65.         # regular falling
  66.         if self.velocity.y >= 0:
  67.            
  68.             self.velocity -= gravity * self.up_direction * delta
  69.            
  70.        
  71.         #decelerating from a jump
  72.         if _jumping:
  73.            
  74.             jump_force -= down_force
  75.            
  76.             var jump : Vector3= jump_force * self.up_direction * delta
  77.            
  78.             self.velocity.y += jump_force
  79.        
  80.         #clamp speed to prevent decent from going over terminal velocity
  81.         #we use maxf since we dont need a minium value for velocity
  82.        
  83.         self.velocity.y = maxf(self.velocity.y, -terminal_velocity)
  84.    
  85.     #reset when on floor
  86.     if is_on_floor():
  87.        
  88.         _jumping = false
  89.         jump_force = _intial_jump_force
  90.    
  91.    
  92.     move_and_slide()
  93.  
Advertisement
Add Comment
Please, Sign In to add comment