Advertisement
Ragdev

Player/Main Script

Nov 12th, 2023 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 1.51 KB | Software | 0 0
  1. extends CharacterBody3D
  2.  
  3. @onready var camera = $Cam/Camera
  4. @onready var cam = $Cam
  5.  
  6. const MOUSE_SENSITIVITY = 0.25
  7. const GRAVITY = -20.0
  8. const JUMP_SPEED = 4.5
  9. const SPEED = 6
  10. const ACCEL = 12.0
  11. const AIR_ACCEL = 1.5
  12.  
  13. var vel = Vector3.ZERO
  14. var currentvel = Vector3.ZERO
  15. var dir = Vector3.ZERO
  16.  
  17. func _ready():
  18.     Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  19.  
  20. func _physics_process(delta):
  21.     dir = Vector3.ZERO
  22.     if Input.is_action_pressed("Forward"):
  23.         dir -= camera.global_transform.basis.z
  24.     if Input.is_action_pressed("Back"):
  25.         dir += camera.global_transform.basis.z
  26.     if Input.is_action_pressed("Left"):
  27.         dir -= camera.global_transform.basis.x
  28.     if Input.is_action_pressed("Right"):
  29.         dir += camera.global_transform.basis.x
  30.    
  31.     dir = dir.normalized()
  32.    
  33.     var target_vel = dir * SPEED
  34.     var accel = ACCEL if is_on_floor() else AIR_ACCEL
  35.    
  36.     currentvel = currentvel.lerp(target_vel, accel * delta)
  37.    
  38.     vel.x = currentvel.x
  39.     vel.z = currentvel.z
  40.    
  41.     set_velocity(vel)
  42.     set_up_direction(Vector3.UP)
  43.     set_floor_stop_on_slope_enabled(true)
  44.     set_max_slides(4)
  45.     set_floor_max_angle(deg_to_rad(45))
  46.     move_and_slide()
  47.     vel = velocity
  48.  
  49.     vel.y += GRAVITY * delta
  50.     if Input.is_action_just_pressed("Jump") and is_on_floor():
  51.         vel.y = JUMP_SPEED
  52.        
  53. func _input(event):
  54.     if event is InputEventMouseMotion:
  55.         cam.rotate_x(deg_to_rad(event.relative.y * MOUSE_SENSITIVITY * 1))
  56.         cam.rotation_degrees.x = clamp(cam.rotation_degrees.x, -40, 35)
  57.         self.rotate_y(deg_to_rad(event.relative.x * MOUSE_SENSITIVITY * -1))
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement