Advertisement
Guest User

3d platformer godot script

a guest
Nov 20th, 2018
12,703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3. const MOVE_SPEED = 12
  4. const JUMP_FORCE = 30
  5. const GRAVITY = 0.98
  6. const MAX_FALL_SPEED = 30
  7.  
  8. const H_LOOK_SENS = 1.0
  9. const V_LOOK_SENS = 1.0
  10.  
  11. onready var cam = $CamBase
  12. onready var anim = $Graphics/AnimationPlayer
  13.  
  14. var y_velo = 0
  15.  
  16. func _ready():
  17. anim.get_animation("walk").set_loop(true)
  18. #Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  19.  
  20. func _input(event):
  21. if event is InputEventMouseMotion:
  22. cam.rotation_degrees.x -= event.relative.y * V_LOOK_SENS
  23. cam.rotation_degrees.x = clamp(cam.rotation_degrees.x, -90, 90)
  24. rotation_degrees.y -= event.relative.x * H_LOOK_SENS
  25.  
  26. func _physics_process(delta):
  27. var move_vec = Vector3()
  28. if Input.is_action_pressed("move_forwards"):
  29. move_vec.z -= 1
  30. if Input.is_action_pressed("move_backwards"):
  31. move_vec.z += 1
  32. if Input.is_action_pressed("move_right"):
  33. move_vec.x += 1
  34. if Input.is_action_pressed("move_left"):
  35. move_vec.x -= 1
  36. move_vec = move_vec.normalized()
  37. move_vec = move_vec.rotated(Vector3(0, 1, 0), rotation.y)
  38. move_vec *= MOVE_SPEED
  39. move_vec.y = y_velo
  40. move_and_slide(move_vec, Vector3(0, 1, 0))
  41.  
  42. var grounded = is_on_floor()
  43. y_velo -= GRAVITY
  44. var just_jumped = false
  45. if grounded and Input.is_action_just_pressed("jump"):
  46. just_jumped = true
  47. y_velo = JUMP_FORCE
  48. if grounded and y_velo <= 0:
  49. y_velo = -0.1
  50. if y_velo < -MAX_FALL_SPEED:
  51. y_velo = -MAX_FALL_SPEED
  52.  
  53. if just_jumped:
  54. play_anim("jump")
  55. elif grounded:
  56. if move_vec.x == 0 and move_vec.z == 0:
  57. play_anim("idle")
  58. else:
  59. play_anim("walk")
  60.  
  61. func play_anim(name):
  62. if anim.current_animation == name:
  63. return
  64. anim.play(name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement