Guest User

movement script

a guest
Apr 8th, 2023
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends CharacterBody3D
  2.  
  3. #movement variables
  4. @export var speed := 9
  5. @export var sprint_speed := 12
  6. @export var acceleration := 10
  7. @export var air_acceleration := 4
  8. var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
  9. @export var fall_acceleration := 4
  10. @export var jump_force := 12
  11.  
  12. #camera variables
  13. @onready var camera := $Camera3D
  14. @onready var move_speed_label := $Camera3D/PlayerUi/GameInfo/PlayerSpeed/MoveSpeed
  15. @onready var fps_counter := $Camera3D/PlayerUi/GameInfo/FpsCounter
  16. @export var camera_sensitivity := 0.25
  17. @export var camera_minimum_clamp := -90
  18. @export var camera_maximum_clamp := 90
  19.  
  20. var velocity_value := Vector3.ZERO
  21. var collision_info
  22.  
  23. #capture mouse
  24. func _ready() -> void:
  25.     Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
  26.  
  27. #mouse inputs
  28. func _input(event: InputEvent) -> void:
  29.     if event is InputEventMouseMotion:
  30.         rotate_y(event.relative.x * camera_sensitivity * -0.01)
  31.         camera.rotate_x(event.relative.y * camera_sensitivity * -0.01)
  32.         camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(camera_minimum_clamp), deg_to_rad(camera_maximum_clamp))
  33.  
  34. #ui stats
  35. func _process(_delta: float) -> void:
  36.     velocity_value.y = 0
  37.     move_speed_label.text = "Speed : " + "%.2f" % (velocity_value.length())
  38.     fps_counter.text = "Fps : " + str(Engine.get_frames_per_second())
  39.  
  40. #player mopvement
  41. func _physics_process(delta: float) -> void:
  42.     var input_direction := Vector3.ZERO
  43.    
  44.     #get inputs
  45.     input_direction.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
  46.     input_direction.z = Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward")
  47.    
  48.     var direction = (transform.basis * Vector3(input_direction.x, 0, input_direction.z)).normalized()
  49.    
  50.     if Input.is_action_just_pressed("jump") and is_on_floor():
  51.         velocity.y = jump_force
  52.    
  53.     if direction != Vector3.ZERO:
  54.         $Character.look_at(position + direction, Vector3.UP)
  55.    
  56.     #player movement velocity
  57.     if Input.is_action_pressed("sprint"):
  58.         velocity.x = lerp(velocity.x, direction.x * sprint_speed, acceleration * delta)
  59.         velocity.z = lerp(velocity.z, direction.z * sprint_speed, acceleration * delta)
  60.     elif not is_on_floor():
  61.         velocity.x = lerp(velocity.x, direction.x * speed, air_acceleration * delta)
  62.         velocity.z = lerp(velocity.z, direction.z * speed, air_acceleration * delta)
  63.     else:
  64.         velocity.x = lerp(velocity.x, direction.x * speed, acceleration * delta)
  65.         velocity.z = lerp(velocity.z, direction.z * speed, acceleration * delta)
  66.    
  67.     #falling velocity
  68.     if not is_on_floor():
  69.         velocity.y = lerp(velocity.y, velocity.y - (gravity), fall_acceleration * delta)
  70.    
  71.     velocity_value = velocity
  72.     move_and_slide()
  73.    
  74.  
Advertisement
Add Comment
Please, Sign In to add comment