chris33556

updated_3d_script_07_07_2022

Jul 7th, 2022
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3. var max_speed = 60
  4. var gravity = 120
  5. var jump_impulse = 55
  6. var jump = false
  7. var sprite_node
  8.  
  9.  
  10. var velocity = Vector3.ZERO
  11.  
  12.  
  13. func _ready():
  14.  
  15. sprite_node = get_node("Sprite3D")
  16.  
  17. func _physics_process(delta):
  18. var input_vector = get_input_vector()
  19. apply_movement(input_vector)
  20. apply_gravity(delta)
  21. jump()
  22. velocity = move_and_slide(velocity, Vector3.UP)
  23.  
  24.  
  25. func get_input_vector():
  26. var input_vector = Vector3.ZERO
  27. input_vector.x = int(Input.is_action_pressed("move_right")) - int(Input.is_action_pressed("move_left"))
  28. input_vector.z = int(Input.is_action_pressed("move_back")) - int(Input.is_action_pressed("move_forward"))
  29.  
  30. return input_vector.normalized()
  31.  
  32.  
  33. func apply_movement(input_vector):
  34.  
  35. if is_on_floor(): #this command is_on_floor and jump == false prevents--
  36. jump == false #--- the player moving left-right in mid-air
  37. velocity.x = input_vector.x * max_speed
  38. velocity.z = input_vector.z * max_speed
  39.  
  40. if velocity.x > 0: #setting a variable "sprite_node" enables
  41. sprite_node.set_flip_h(false) #the sprite to be called and flipped
  42. elif velocity.x < 0: # in the command line
  43. sprite_node.set_flip_h(true)
  44.  
  45.  
  46. func apply_gravity(delta):
  47. velocity.y -= gravity * delta
  48.  
  49.  
  50. func jump():
  51.  
  52. if is_on_floor() and Input.is_action_just_pressed("jump"):
  53. jump = true
  54. velocity.y = jump_impulse
  55.  
Advertisement
Add Comment
Please, Sign In to add comment