Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2021
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody
  2.  
  3. var movement_amplifier = 10
  4. var velocity = Vector3()
  5. var desired_velocity = Vector3()
  6. var friction = 0.1
  7. var gravity_strength = -60
  8. var facing_direction = Vector3()
  9. var direction = Vector3()
  10. var upwards_direction = Vector3(0, 1, 0)
  11.  
  12. func right_of():
  13.     return -facing_direction.rotated(Vector3(0, 1, 0), deg2rad(-90))
  14.  
  15. func left_of():
  16.     return -facing_direction.rotated(Vector3(0, 1, 0), deg2rad(90))
  17.  
  18. var highest_reached = 0
  19.  
  20. func _process(delta):
  21. #   Gravity and deacceleration
  22.     velocity.y+=gravity_strength*delta
  23.     desired_velocity=Vector3(0, 0, 0)
  24.    
  25. #   Variable friction
  26.     if is_on_floor():
  27.         friction = 10
  28.     else:
  29.         friction = 1
  30.     friction*=delta
  31.  
  32. #   Input handling
  33.     if Input.is_action_pressed("forward"):
  34.         desired_velocity += -facing_direction * movement_amplifier
  35.     if Input.is_action_pressed("backward"):
  36.         desired_velocity += facing_direction * movement_amplifier
  37.     if Input.is_action_pressed("left"):
  38.         desired_velocity += left_of() * movement_amplifier
  39.     if Input.is_action_pressed("right"):
  40.         desired_velocity += right_of() * movement_amplifier
  41.     if Input.is_action_pressed("jump") and is_on_floor():
  42.         velocity.y += 4*movement_amplifier
  43.         desired_velocity.y += 4*movement_amplifier
  44.     facing_direction = global_transform.basis.z
  45.  
  46. #   Gradually go from your velocity, to your desired velocity - thanks to lerp()
  47.     velocity = move_and_slide(lerp(velocity, desired_velocity, friction), upwards_direction)
  48.  
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement