Guest User

Untitled

a guest
May 1st, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. extends CharacterBody3D
  2.  
  3. var speed
  4. const WALK_SPEED = 5.0
  5. const SPRINT_SPEED = 8.0
  6. const JUMP_VELOCITY = 4.8
  7. const SENSITIVITY = 0.004
  8.  
  9. #bob variables
  10. const BOB_FREQ = 2.4
  11. const BOB_AMP = 0.08
  12. var t_bob = 0.0
  13.  
  14. #fov variables
  15. const BASE_FOV = 70
  16. const FOV_CHANGE = 1.5
  17.  
  18. var gravity = 9.8
  19.  
  20. @onready var camera = $CharRoot/Model/Armature/Skeleton3D/BoneAttachment3D/CameraRotate/Camera
  21. @onready var cam_rotate = $CharRoot/Model/Armature/Skeleton3D/BoneAttachment3D/CameraRotate
  22. @onready var anim_tree = $AnimationTree
  23.  
  24.  
  25. func _ready():
  26. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  27.  
  28.  
  29. func _unhandled_input(event):
  30. if event is InputEventMouseMotion:
  31. rotate_y(-event.relative.x * SENSITIVITY)
  32. cam_rotate.rotate_x(-event.relative.y * SENSITIVITY)
  33. cam_rotate.rotation.x = clamp(cam_rotate.rotation.x, deg_to_rad(-40), deg_to_rad(60))
  34.  
  35. """
  36.  
  37.  
  38. cam_rotate.rotate_x(deg_to_rad(event.relative.y * SENSITIVITY))
  39. rotate_y(deg_to_rad(event.relative.x * SENSITIVITY * -1))
  40.  
  41. var camera_rot = cam_rotate.rotation_degrees
  42. camera_rot.x = clamp(camera_rot.x, -70, 70)
  43. cam_rotate.rotation_degrees = camera_rot
  44. """
  45.  
  46.  
  47. func _physics_process(delta):
  48. # Add the gravity.
  49. if not is_on_floor():
  50. velocity.y -= gravity * delta
  51.  
  52. # Handle Jump.
  53. if Input.is_action_just_pressed("jump") and is_on_floor():
  54. velocity.y = JUMP_VELOCITY
  55.  
  56. # Handle Sprint.
  57. if Input.is_action_pressed("sprint"):
  58. speed = SPRINT_SPEED
  59. else:
  60. speed = WALK_SPEED
  61.  
  62. # Get the input direction and handle the movement/deceleration.
  63. var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
  64. var direction = (camera.transform.basis * transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
  65. if is_on_floor():
  66. if direction:
  67. velocity.x = direction.x * speed
  68. velocity.z = direction.z * speed
  69. else:
  70. velocity.x = lerp(velocity.x, direction.x * speed, delta * 7.0)
  71. velocity.z = lerp(velocity.z, direction.z * speed, delta * 7.0)
  72. else:
  73. velocity.x = lerp(velocity.x, direction.x * speed, delta * 3.0)
  74. velocity.z = lerp(velocity.z, direction.z * speed, delta * 3.0)
  75.  
  76. # Head bob
  77. "t_bob += delta * velocity.length() * float(is_on_floor())"
  78. "camera.transform.origin = _headbob(t_bob)"
  79.  
  80. # FOV
  81. var velocity_clamped = clamp(velocity.length(), 0.5, SPRINT_SPEED * 2)
  82. var target_fov = BASE_FOV + FOV_CHANGE * velocity_clamped
  83. camera.fov = lerp(camera.fov, target_fov, delta * 8.0)
  84.  
  85. anim_tree.set("parameters/conditions/idle", input_dir==Vector2.ZERO && is_on_floor())
  86. anim_tree.set("parameters/conditions/walk", (input_dir.y == 1 || input_dir.y == -1) && is_on_floor())
  87. anim_tree.set("parameters/conditions/sraifLeft", input_dir.x == -1 && is_on_floor())
  88. anim_tree.set("parameters/conditions/sraifRight", input_dir.x == 1 && is_on_floor())
  89. anim_tree.set("parameters/conditions/falling", !is_on_floor())
  90. anim_tree.set("parameters/conditions/landed", is_on_floor())
  91. move_and_slide()
  92.  
  93.  
  94. func _headbob(time) -> Vector3:
  95. var pos = Vector3.ZERO
  96. pos.y = sin(time * BOB_FREQ) * BOB_AMP
  97. pos.x = cos(time * BOB_FREQ / 2) * BOB_AMP
  98. return pos
  99.  
Advertisement
Add Comment
Please, Sign In to add comment