extends CharacterBody3D #class name classname PlayerCharacter #states variables enum…
- User
extends CharacterBody3D
#class name class_name PlayerCharacter
#states variables enum states { IDLE, WALK, RUN, CROUCH, SLIDE, JUMP, INAIR, ONWALL, DASH } var currentState
#move variables @export_group("move variables") var moveSpeed : float var desiredMoveSpeed : float @export var maxSpeed : float @export var walkSpeed : float @export var runSpeed : float @export var crouchSpeed : float var slideSpeed : float @export var slideSpeedAddon : float @export var dashSpeed : float var moveAcceleration : float @export var walkAcceleration : float @export var runAcceleration : float @export var crouchAcceleration : float var moveDecceleration : float @export var walkDecceleration : float @export var runDecceleration : float @export var crouchDecceleration : float @export var inAirMoveSpeed : float
- Assistant
- User
#movement variables @export_group("movement variables") var inputDirection : Vector2 var moveDirection : Vector3 @export var hitGroundCooldown : float #amount of time the character keep his accumulated speed before losing it (while being on ground) var hitGroundCooldownRef : float var lastFramePosition : Vector3 var floorAngle #angle of the floor the character is on var slopeAngle #angle of the slope the character is on var canInput : bool var collisionInfo
#jump variables @export_group("jump variables") @export var jumpHeight : float @export var jumpTimeToPeak : float @export var jumpTimeToFall : float @onready var jumpVelocity : float = (2.0 * jumpHeight) / jumpTimeToPeak @export var jumpCooldown : float @export var jumpCooldownRef : float @export var nbJumpsInAirAllowed : int @export var nbJumpsInAirAllowedRef : int
- Assistant
- User
func _ready(): #set the start move speed moveSpeed = walkSpeed moveAcceleration = walkAcceleration moveDecceleration = walkDecceleration
#set the values refenrencials for the needed variables desiredMoveSpeed = moveSpeed jumpCooldownRef = jumpCooldown nbJumpsInAirAllowedRef = nbJumpsInAirAllowed hitGroundCooldownRef = hitGroundCooldown slideTimeRef = slideTime dashTimeRef = dashTime timeBeforeCanSlideAgainRef = timeBeforeCanSlideAgain timeBeforeCanDashAgainRef = timeBeforeCanDashAgain canWallRun = false canInput = true #disable the crouch hitbox, enable is standing one if !crouchHitbox.disabled: crouchHitbox.disabled = true if standHitbox.disabled: standHitbox.disabled = false #set the mesh scale of the character mesh.scale = Vector3(1.0, 1.0, 1.0) - Assistant
- User
func _process(_delta): #the behaviours that is preferable to check every "visual" frame
inputManagement() displayStats()func _physics_process(delta): #the behaviours that is preferable to check every "physics" frame
applies(delta) move(delta) collisionHandling() move_and_slide() - Assistant
- User
func inputManagement(): #for each state, check the possibles actions available #This allow to have a good control of the controller behaviour, because you can easely check the actions possibls, #add or remove some, and it prevent certain actions from being played when they shouldn't be
if canInput: match currentState: states.IDLE: if Input.is_action_just_pressed("jump"): jump(0.0, false) if Input.is_action_just_pressed("crouch | slide"): crouchStateChanges() states.WALK: if Input.is_action_just_pressed("run"): runStateChanges() if Input.is_action_just_pressed("jump"): jump(0.0, false) if Input.is_action_just_pressed("crouch | slide"): crouchStateChanges() if Input.is_action_just_pressed("dash"): dashStateChanges() states.RUN: if Input.is_action_just_pressed("run"): walkStateChanges() if Input.is_action_just_pressed("jump"): jump(0.0, false) if Input.is_action_just_pressed("crouch | slide"): slideStateChanges() if Input.is_action_just_pressed("dash"): dashStateChanges() states.CROUCH: if Input.is_action_just_pressed("run") and !ceilingCheck.is_colliding(): walkStateChanges() if Input.is_action_just_pressed("crouch | slide") and !ceilingCheck.is_colliding(): walkStateChanges() states.SLIDE: if Input.is_action_just_pressed("run"): runStateChanges() if Input.is_action_just_pressed("jump"): jump(0.0, false) if Input.is_action_just_pressed("crouch | slide"): runStateChanges() states.JUMP: if Input.is_action_just_pressed("crouch | slide"): slideStateChanges() if Input.is_action_just_pressed("dash"): dashStateChanges() if Input.is_action_just_pressed("jump"): jump(0.0, false) states.INAIR: if Input.is_action_just_pressed("crouch | slide"): slideStateChanges() if Input.is_action_just_pressed("dash"): dashStateChanges() if Input.is_action_just_pressed("jump"): jump(0.0, false) states.ONWALL: if Input.is_action_just_pressed("jump"): jump(0.0, false) states.DASH: pass - Assistant
- User
func displayStats(): #call the functions in charge of displaying the controller properties hud.displayCurrentState(currentState) hud.displayMoveSpeed(moveSpeed) hud.displayDesiredMoveSpeed(desiredMoveSpeed) hud.displayVelocity(velocity.length()) hud.displayNbJumpsAllowedInAir(nbJumpsInAirAllowed)
#not a property, but a visual if currentState == states.DASH: hud.displaySpeedLines(dashTime) - Assistant
- User
- Assistant
- User
- Assistant
- User
func jump(jumpBoostValue : float, isJumpBoost : bool): #this function manage the jump behaviour, depending of the different variables and states the character is
var canJump : bool = false #jump condition #on wall jump if is_on_wall() and canWallRun: currentState = states.JUMP velocity = get_wall_normal() * wallJumpVelocity #add some knockback in the opposite direction of the wall velocity.y = jumpVelocity jumpCooldown = jumpCooldownRef else: #in air jump if !is_on_floor(): if jumpCooldown <= 0: #if the character jump from a jumppad, the jump isn't taken into account in the max numbers of jumps allowed, allowing the character to continusly jump as long as it lands on a jumppad if (nbJumpsInAirAllowed > 0) or (nbJumpsInAirAllowed <= 0 and isJumpBoost): if !isJumpBoost: nbJumpsInAirAllowed -= 1 jumpCooldown = jumpCooldownRef canJump = true #on floor jump else: jumpCooldown = jumpCooldownRef canJump = true #apply jump if canJump: if isJumpBoost: nbJumpsInAirAllowed = nbJumpsInAirAllowedRef currentState = states.JUMP velocity.y = jumpVelocity + jumpBoostValue #apply directly jump velocity to y axis velocity, to give the character instant vertical forcez canJump = false#theses functions manages the differents changes and appliments the character will go trought when changing his current state
- Assistant
- User
- Assistant