Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2022
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3.  
  4.  
  5. export var linear_velocity = Vector3.ZERO
  6.  
  7. var velocity = Vector3.ZERO
  8. var jump = 5
  9. var accel = ground_acceleration
  10. var deaccel = ground_deceleration
  11. var speed = move_speed
  12.  
  13.  
  14. var gravity = 10
  15. export var jump_height : float = 10
  16. export var jump_time_to_peak : float = 0.5
  17. export var jump_time_to_descent : float = 0.1
  18.  
  19.  
  20. onready var jump_gravity :float = (-2.0 * jump_height) / (jump_time_to_peak * jump_time_to_peak)
  21. onready var jump_velocity :float = (2.0 * jump_height) / jump_time_to_peak
  22. onready var fall_gravity : float = (-2.0 * jump_height) / (jump_time_to_descent * jump_time_to_descent)
  23.  
  24.  
  25. export var move_speed := 15
  26. export var ground_acceleration := 15
  27. export var ground_deceleration := 15
  28. export var air_acceleration := 30
  29. export var air_deceleration := 0
  30. export var air_speed = 100
  31. export var mouse_sens = 0.2
  32.  
  33. onready var look_pivot : Spatial = $Head
  34.  
  35. var gravity_local: Vector3 = Vector3()
  36.  
  37.  
  38. func _input(event):
  39. if event is InputEventMouseMotion:
  40. rotate_y(deg2rad(-1 * event.relative.x) * mouse_sens)
  41. look_pivot.rotate_x(deg2rad(-1 * event.relative.y) * mouse_sens )
  42. look_pivot.rotation.x = clamp(look_pivot.rotation.x, deg2rad(-90), deg2rad(90))
  43.  
  44.  
  45.  
  46.  
  47. func _physics_process(delta):
  48. var move_direction := Vector3(0, 0, 0)
  49.  
  50. if not is_on_floor():
  51. gravity_local += gravity * Vector3.DOWN * delta
  52. else:
  53. gravity_local = gravity * -get_floor_normal() * delta
  54.  
  55.  
  56.  
  57. if Input.is_action_just_pressed("jump") and is_on_floor():
  58. gravity_local.y = jump
  59.  
  60. if Input.is_action_pressed("move_forward"):
  61. move_direction.z -= Input.get_action_strength("move_forward")
  62.  
  63. if Input.is_action_pressed("move_backward"):
  64. move_direction.z += Input.get_action_strength("move_backward")
  65.  
  66. if Input.is_action_pressed("move_left"):
  67. move_direction.x -= Input.get_action_strength("move_left")
  68.  
  69. if Input.is_action_pressed("move_right"):
  70. move_direction.x += Input.get_action_strength("move_right")
  71.  
  72. move_direction = move_direction.normalized()
  73. move_direction = move_direction.rotated(Vector3.UP, rotation.y)
  74.  
  75. if is_on_floor():
  76. accel = ground_acceleration
  77. deaccel = ground_deceleration
  78. speed = move_speed
  79. else:
  80. accel = air_acceleration
  81. deaccel = air_deceleration
  82. speed = air_speed
  83.  
  84. # Ground movement
  85. if move_direction.length() > 0:
  86. linear_velocity = linear_velocity.linear_interpolate(move_direction * move_speed, accel * delta)
  87.  
  88. else:
  89. linear_velocity = linear_velocity.linear_interpolate(Vector3.ZERO, deaccel * delta)
  90.  
  91.  
  92.  
  93. linear_velocity = move_and_slide(linear_velocity + gravity_local, Vector3.UP)
  94.  
  95.  
  96. func get_gravity() -> float:
  97. return jump_gravity if velocity.y < 0.0 else fall_gravity
  98.  
  99.  
  100.  
  101. func jump():
  102. linear_velocity.y = jump_velocity
  103.  
  104. # Called when the node enters the scene tree for the first time.
  105. func _ready():
  106. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) # Replace with function body.
  107.  
  108.  
  109. # Called every frame. 'delta' is the elapsed time since the previous frame.
  110. #func _process(delta):
  111. # pass
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement