Advertisement
nezvers

Godot 3D slope movement

Jun 23rd, 2020
1,506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3. export (float) var walking_speed = 100
  4. export (float) var jump_force = 30
  5. export (Vector3) var gravity = Vector3(0, -50, 0)
  6.  
  7. export(float, 0.1, 1.0) var sensitivity_x = 0.6
  8. export(float, 0.1, 1.0) var sensitivity_y = 0.6
  9.  
  10. export (float) var acceleration = 80.0
  11.  
  12. var velocity := Vector3.ZERO
  13. var is_grounded = false
  14.  
  15. var mouse_motion = Vector2()
  16. onready var camera = $Camera
  17.  
  18.  
  19. func _ready() -> void:
  20. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  21.  
  22.  
  23. func _input(event : InputEvent) -> void:
  24. if event is InputEventMouseMotion:
  25. mouse_motion = event.relative
  26. elif event.is_action_pressed("ui_cancel"):
  27. get_tree().quit()
  28.  
  29.  
  30. func handle_camera_rotation(delta : float) -> void:
  31. rotate_y( (PI*0.1) * - mouse_motion.x * sensitivity_x * delta)
  32. camera.rotate_x( (PI*0.1) * -mouse_motion.y * sensitivity_y * delta )
  33. camera.rotation.x = clamp(camera.rotation.x, -PI*0.5, PI*0.5)
  34. mouse_motion = Vector2()
  35.  
  36.  
  37. func _physics_process(delta : float) -> void:
  38. handle_camera_rotation(delta)
  39.  
  40. var dir = Vector3.ZERO
  41.  
  42. if Input.is_action_pressed("move_forward"):
  43. dir -= transform.basis.z
  44. if Input.is_action_pressed("move_back"):
  45. dir += transform.basis.z
  46.  
  47. if Input.is_action_pressed("move_left"):
  48. dir -= transform.basis.x
  49. if Input.is_action_pressed("move_right"):
  50. dir += transform.basis.x
  51.  
  52. dir = dir.normalized() * walking_speed
  53. velocity = velocity.move_toward(Vector3(dir.x, velocity.y, dir.z), acceleration*delta)
  54.  
  55. velocity += gravity * delta
  56.  
  57. is_grounded = is_on_floor()
  58.  
  59. if is_grounded and Input.is_action_just_pressed("jump"):
  60. velocity.y = jump_force
  61. is_grounded = false
  62.  
  63. var snap:Vector3
  64. if !is_grounded:
  65. snap = Vector3.ZERO
  66. else:
  67. snap = Vector3.DOWN * 0.2
  68. velocity = move_and_slide_with_snap(velocity, snap, Vector3.UP, true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement