Advertisement
Guest User

Funky player controller?

a guest
Nov 17th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends CharacterBody3D
  2.  
  3. @export var move_rampup_curve_tex: CurveTexture = preload("res://player/move_rampup_curve.curvetex")
  4. @onready var move_rampup_curve = move_rampup_curve_tex.get_curve()
  5. @export var move_rampup_speed: float = 10
  6. @export var speed: float = 5.0
  7. var current_move_rampup_x: float = 0
  8. var current_move_rampup_z: float = 0
  9. @export var jump_velocity: float = 4.5
  10.  
  11. @export var camera_boom: SpringArm3D
  12. @export var mouse_sens: float = 1
  13. @export var vertical_look_limit = 90
  14. @export var shoulder_offset: float = 0
  15.  
  16. func _ready() -> void:
  17.     Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
  18.  
  19. func _input(event: InputEvent) -> void:
  20.     if event is InputEventMouseMotion:
  21.         var mouse_delta = event.screen_relative
  22.         rotate_y(deg_to_rad(-mouse_delta.x * mouse_sens)) #Should be rotating player?????
  23.         camera_boom.rotation_degrees = Vector3(clamp(camera_boom.rotation_degrees.x + (-mouse_delta.y * mouse_sens), -vertical_look_limit, vertical_look_limit),
  24.                                                     rotation_degrees.y,
  25.                                                     rotation_degrees.z
  26.                                                 )
  27.         Input.warp_mouse(Vector2(get_viewport().size.x/2, get_viewport().size.y/2))
  28.  
  29. func _physics_process(delta: float) -> void:
  30.    
  31.     # Add the gravity.
  32.     if not is_on_floor():
  33.         velocity += get_gravity() * delta
  34.  
  35.     # Handle jump.
  36.     if Input.is_action_just_pressed("jump") and is_on_floor():
  37.         velocity.y = jump_velocity
  38.  
  39.     # Get the input direction and handle the movement/deceleration.
  40.     # As good practice, you should replace UI actions with custom gameplay actions.
  41.     var input_dir := Input.get_vector("strafe_left", "strafe_right", "move_forward", "move_back")
  42.     var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
  43.    
  44.     if input_dir.x != 0:
  45.         current_move_rampup_x += move_toward(0, 1, delta * move_rampup_speed)
  46.     else:
  47.         current_move_rampup_x = 0
  48.    
  49.     if input_dir.y != 0:
  50.         current_move_rampup_z += move_toward(0, 1, delta * move_rampup_speed)
  51.     else:
  52.         current_move_rampup_z = 0
  53.    
  54.     if direction:
  55.         velocity.x = direction.x * speed * move_rampup_curve.sample(current_move_rampup_x)
  56.         velocity.z = direction.z * speed * move_rampup_curve.sample(current_move_rampup_z)
  57.     else:
  58.         velocity.x = move_toward(velocity.x, 0, speed)
  59.         velocity.z = move_toward(velocity.z, 0, speed)
  60.  
  61.     move_and_slide()
  62.  
Tags: #godot #Godot4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement