Advertisement
Chillaxe

Character Code

Feb 21st, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 3.61 KB | Gaming | 0 0
  1. extends CharacterBody3D
  2.  
  3.  
  4. var SPEED = 10.0
  5. var StandingSpeed = 10.0
  6. var JUMP_VELOCITY = 20.0
  7. var gravity = 70.0
  8. var MouseMovement = Vector2(0.0, 0.0)
  9. var Sensitivity = 0.01
  10. var DiveSpeed = 32.0
  11. var ControllerSensitivity = 2.5
  12. var DiveHeight = 5.0
  13. var GDiveSpeed = 26.0
  14. var GDiveHeight = 8.0
  15. var BDiveSpeed = 60.0
  16. var BDiveHeight = 15.0
  17. var DiveWait = 0.5
  18. var DiveTimer = 0.0
  19. var TerminalVelocity = 40.0
  20. var NormalTerm = 40.0
  21. var WallTerm = 5.0
  22. var CameraNode
  23. var CrouchSpeed = 2.5
  24. var Acceleration = 60.0
  25. var NormalAcceleration = 60.0
  26. var Deceleration = 100.0
  27. var DashDeceleration = 70.0
  28. var NormalDeceleration = 100.0
  29. var IsBombing = false
  30. var IsJumping = false
  31. var IsWallSliding = false
  32. var BombDiveTimer = 0.0
  33. var BombDiveWait = 0.15
  34. func _ready():
  35.     CameraNode = get_node("Node3D")
  36. func _physics_process(delta):
  37.    
  38.     #handle gravity and jump
  39.     if not is_on_floor() and velocity.y > -TerminalVelocity:
  40.         velocity.y -= gravity * delta
  41.     if Input.is_action_pressed("jump") and (is_on_floor() or IsWallSliding) and not IsJumping:
  42.         velocity.y = JUMP_VELOCITY
  43.         IsJumping = true
  44.     if Input.is_action_just_released("jump"):
  45.         IsJumping = false
  46.    
  47.     #handle input
  48.     var input_dir = Input.get_vector("control_left", "control_right", "control_up", "control_down")
  49.     Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
  50.    
  51.     #handle movement
  52.     var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
  53.     if direction:
  54.         velocity.x = move_toward(velocity.x, direction.x * SPEED, delta * Acceleration)
  55.         velocity.z = move_toward(velocity.z, direction.z * SPEED, delta * Acceleration)
  56.     else:
  57.         velocity.x = move_toward(velocity.x, 0, delta * Deceleration)
  58.         velocity.z = move_toward(velocity.z, 0, delta * Deceleration)
  59.    
  60.     #handle bomb
  61.     if Input.is_action_just_pressed("bomb") and not is_on_floor() and not IsBombing:
  62.         IsBombing = true
  63.         velocity.y = -TerminalVelocity
  64.     if is_on_floor():
  65.         if IsBombing:
  66.             IsBombing = false
  67.             BombDiveTimer = BombDiveWait
  68.         Deceleration = NormalDeceleration
  69.         Acceleration = NormalAcceleration
  70.     #handle crouch
  71.     if Input.is_action_just_pressed("crouch"):
  72.         get_node("CollisionShape3D").get_shape().height = 0.4
  73.         get_node("MeshInstance3D").get_mesh().height = 1.0
  74.         SPEED = CrouchSpeed
  75.     if Input.is_action_just_released("crouch"):
  76.         get_node("CollisionShape3D").get_shape().height = 1.0
  77.         get_node("MeshInstance3D").get_mesh().height = 1.6
  78.         SPEED = StandingSpeed
  79.    
  80.     #handle dive
  81.     if direction and Input.is_action_just_pressed("dive") and DiveTimer < 0:
  82.         if is_on_floor():
  83.             if BombDiveTimer >= 0:
  84.                 velocity.x += BDiveSpeed * direction.x
  85.                 velocity.z += BDiveSpeed * direction.z
  86.                 velocity.y += BDiveHeight
  87.             else:
  88.                 velocity.x += GDiveSpeed * direction.x
  89.                 velocity.z += GDiveSpeed * direction.z
  90.                 velocity.y += GDiveHeight
  91.         else:
  92.             velocity.x += DiveSpeed * direction.x
  93.             velocity.z += DiveSpeed * direction.z
  94.             velocity.y += DiveHeight
  95.         DiveTimer = DiveWait
  96.         Deceleration = DashDeceleration
  97.         Acceleration = DashDeceleration
  98.     DiveTimer -= delta
  99.     BombDiveTimer -= delta
  100.     #handle rotation
  101.     rotate(Vector3.UP, MouseMovement.x * -Sensitivity)
  102.     rotate(Vector3.UP, Input.get_axis("camera_left", "camera_right") * delta * -ControllerSensitivity)
  103.    
  104.     move_and_slide()
  105.     MouseMovement = Vector2.ZERO
  106.    
  107.     if get_slide_collision_count() != 0 and not is_on_floor():
  108.         TerminalVelocity = WallTerm
  109.         IsWallSliding = true
  110.     else:
  111.         IsWallSliding = false
  112.         TerminalVelocity = NormalTerm
  113.     if Input.is_action_just_pressed("exit"):
  114.         get_tree().quit()
  115. func _input(event):
  116.     if event is InputEventMouseMotion:
  117.         MouseMovement = event.relative
  118.  
Tags: characters
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement