Advertisement
Ragdev

Simple first-person controller

Nov 19th, 2024
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 1.30 KB | Software | 0 0
  1. extends CharacterBody3D
  2.  
  3. @onready var camera = $Cam/Camera3D
  4. @onready var cam = $Cam
  5.  
  6. const MOUSE_SENSITIVITY = 0.25
  7. const GRAVITY = -20.0
  8. const JUMP_SPEED = 7
  9. const SPEED = 6
  10. const ACCEL = 12.0
  11.  
  12. var vel = Vector3.ZERO
  13. var currentvel = Vector3.ZERO
  14. var dir = Vector3.ZERO
  15.  
  16. func _ready():
  17.     Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  18.  
  19. func _physics_process(delta):
  20.     dir = Vector3.ZERO
  21.     if Input.is_action_pressed("forward"):
  22.         dir -= camera.global_transform.basis.z
  23.     if Input.is_action_pressed("back"):
  24.         dir += camera.global_transform.basis.z
  25.     if Input.is_action_pressed("left"):
  26.         dir -= camera.global_transform.basis.x
  27.     if Input.is_action_pressed("right"):
  28.         dir += camera.global_transform.basis.x
  29.    
  30.     dir = dir.normalized()
  31.    
  32.     var target_vel = dir * SPEED
  33.  
  34.     currentvel = currentvel.lerp(target_vel, ACCEL * delta)
  35.    
  36.     vel.x = currentvel.x
  37.     vel.z = currentvel.z
  38.     vel.y += GRAVITY * delta
  39.    
  40.     if Input.is_action_just_pressed("jump") and is_on_floor():
  41.         vel.y = JUMP_SPEED
  42.  
  43.     set_velocity(vel)
  44.     move_and_slide()
  45.  
  46. func _input(event):
  47.     if event is InputEventMouseMotion:
  48.         cam.rotate_x(deg_to_rad(event.relative.y * -MOUSE_SENSITIVITY))
  49.         cam.rotation_degrees.x = clamp(cam.rotation_degrees.x, -70, 45)
  50.         self.rotate_y(deg_to_rad(event.relative.x * -MOUSE_SENSITIVITY))
  51.  
  52. #RAGDEV
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement