Advertisement
Guest User

godotplayermine

a guest
May 3rd, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. extends CharacterBody3D
  2.  
  3. const SPEED = 5.0
  4. const JUMP_VELOCITY = 12
  5.  
  6. @export_group("Camera")
  7. @export_range(0.0, 1.0) var mouse_sensitivity := 0.25
  8.  
  9. @export_group("Movement")
  10. @export var move_speed := 8.0
  11. @export var acceleration := 20.0
  12. @export var rotation_speed := 12.0
  13.  
  14. @onready var _camera_pivot: Node3D = %Camera_Pivot
  15. @onready var _camera: Camera3D = %Camera3D
  16. @onready var _skin: MeshInstance3D = %MeshInstance3D
  17. @onready var _springarm: SpringArm3D = %SpringArm3D
  18.  
  19.  
  20. var _camera_input_direction := Vector2.ZERO
  21. var _last_movement_direction := Vector3.BACK
  22.  
  23.  
  24. func _input(event: InputEvent) -> void:
  25. if event.is_action_pressed("left_click"):
  26. Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
  27. if event.is_action_pressed("ui_cancel"):
  28. Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
  29.  
  30. #camera zoom
  31. if event.is_action_pressed("zoom_in"):
  32. _springarm.spring_length -= 0.2
  33. if event.is_action_pressed("zoom_out"):
  34. _springarm.spring_length += 0.2
  35.  
  36. func _unhandled_input(event: InputEvent) -> void:
  37. var is_camera_motion := (
  38. event is InputEventMouseMotion and
  39. Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED
  40. )
  41. if is_camera_motion:
  42. _camera_input_direction = event.screen_relative * mouse_sensitivity
  43.  
  44.  
  45.  
  46. func _physics_process(delta: float) -> void:
  47. var raw_input := Input.get_vector("left", "right", "back", "forward")
  48. var forward := -(_camera.global_basis.z)
  49. var right := _camera.global_basis.x
  50.  
  51. var move_direction := forward * raw_input.y + right * raw_input.x
  52. move_direction.y = 0.0
  53. move_direction = move_direction.normalized()
  54.  
  55. velocity = velocity.move_toward(move_direction * move_speed, acceleration * delta)
  56.  
  57. # Add the gravity.
  58. if not is_on_floor():
  59. velocity += get_gravity() * delta
  60.  
  61. # Handle jump.
  62. if Input.is_action_just_pressed("ui_accept") and is_on_floor():
  63. velocity.y = JUMP_VELOCITY
  64.  
  65.  
  66. #camera stuff
  67. _camera_pivot.rotation.x -= -(_camera_input_direction.y) * delta
  68. _camera_pivot.rotation.x = clamp(_camera_pivot.rotation.x, -PI / 4.0, PI / 8.0)
  69. _camera_pivot.rotation.y -= _camera_input_direction.x * delta
  70.  
  71. _camera_input_direction = Vector2.ZERO
  72. _springarm.spring_length = clamp(_springarm.spring_length, 0.5, 6)
  73. move_and_slide()
  74.  
  75. if move_direction.length() > 0.2:
  76. _last_movement_direction = move_direction
  77. var target_angle := Vector3.BACK.signed_angle_to(_last_movement_direction, Vector3.UP)
  78. _skin.global_rotation.y = lerp_angle(_skin.rotation.y, target_angle, rotation_speed * delta)
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement