Advertisement
Guest User

Untitled

a guest
Jul 8th, 2021
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody
  2.  
  3. export var gravity = -15.0
  4. export var walk_speed = 8.0
  5. export var run_speed = 16.0
  6. export var jump_speed = 5.0
  7. export var mouse_sensitivity = 0.0015
  8. export var acceleration = 4.0
  9. export var friction = 8.0
  10. export var fall_limit = -1000.0
  11.  
  12. var pivot
  13.  
  14. var playable = true
  15. var dir = Vector3.ZERO
  16. var velocity = Vector3.ZERO
  17. var speedlabel
  18. var footstepPlayer
  19. func _ready():
  20.     pivot = $pivot
  21.     footstepPlayer = $FootstepPlayer
  22.     speedlabel = $"../../../speed"
  23.     Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  24.     mouse_sensitivity *= $"../../".stretch_shrink
  25.  
  26. func _physics_process(delta):
  27.     dir = Vector3.ZERO
  28.     var basis = global_transform.basis
  29.     if Input.is_action_pressed("move_forward"):
  30.         dir -= basis.z
  31.     if Input.is_action_pressed("move_back"):
  32.         dir += basis.z
  33.     if Input.is_action_pressed("move_left"):
  34.         dir -= basis.x
  35.     if Input.is_action_pressed("move_right"):
  36.         dir += basis.x
  37.     dir = dir.normalized()
  38.  
  39.     var speed = walk_speed
  40.     if is_on_floor():
  41.         #this prevents you from sliding without messing up the is_on_ground() check
  42.         velocity.y += gravity * delta / 100.0
  43.         if Input.is_action_pressed("run"):
  44.             speed = run_speed
  45.         if Input.is_action_pressed("jump"):
  46.             velocity.y = jump_speed
  47.     else:
  48.         velocity.y += gravity * delta
  49.  
  50.     var hvel = velocity
  51.     hvel.y = 0.0
  52.  
  53.     var target = dir * speed
  54.     var accel
  55.  
  56.    
  57.    
  58.     if is_on_floor() and !Input.is_action_pressed("jump"):
  59.         accel = friction
  60.         hvel = hvel.linear_interpolate(target, accel * delta)
  61.     else:
  62.         if dir.dot(hvel) > 0: #not sudden turn
  63.             accel = 0
  64.         else :
  65.             if !Input.is_action_pressed("move_forward"):
  66.                 print("air strafe:", (hvel.dot(dir)) )
  67.                 hvel += dir * (walk_speed-(hvel.dot(dir))) * 15 * delta
  68.             else:
  69.                 accel = friction
  70.                 hvel = hvel.linear_interpolate(target, accel * delta)
  71.  
  72.  
  73.  
  74.     velocity.x = hvel.x
  75.     velocity.z = hvel.z
  76.     if playable:
  77.         velocity = move_and_slide(velocity, Vector3.UP, false)
  78.         var _vel = velocity
  79.         _vel.y = 0
  80.         var _hvel = _vel.length()
  81.         if _hvel>0.1 and is_on_floor():
  82.             if !footstepPlayer.playing:
  83.                 footstepPlayer.play()
  84.                 print(footstepPlayer.playing)
  85.         speedlabel.text = String(_hvel) +"   fps:" + str(Performance.get_monitor(Performance.TIME_FPS))
  86.  
  87.     #prevents infinite falling
  88.     if translation.y < fall_limit and playable:
  89.         playable = false
  90.         fader._reload_scene()
  91.  
  92. func _unhandled_input(event):
  93.     if event is InputEventMouseMotion and playable:
  94.         rotate_y(-event.relative.x * mouse_sensitivity)
  95.         pivot.rotate_x(-event.relative.y * mouse_sensitivity)
  96.         pivot.rotation.x = clamp(pivot.rotation.x, -1.2, 1.2)
  97.  
  98.  
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement