cubio.gd 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. extends RigidBody3D
  2. @onready var shape_cast = $ShapeCast3D
  3. @onready var camera = $Target/Camera3D
  4. @onready var start_position = position
  5. func _physics_process(_delta):
  6. if Input.is_action_just_pressed(&"exit"):
  7. get_tree().quit()
  8. if Input.is_action_just_pressed(&"reset_position") or global_position.y < - 6:
  9. # Pressed the reset key or fell off the ground.
  10. position = start_position
  11. linear_velocity = Vector3.ZERO
  12. var dir = Vector3()
  13. dir.x = Input.get_axis(&"move_left", &"move_right")
  14. dir.z = Input.get_axis(&"move_forward", &"move_back")
  15. # Get the camera's transform basis, but remove the X rotation such
  16. # that the Y axis is up and Z is horizontal.
  17. var cam_basis = camera.global_transform.basis
  18. cam_basis = cam_basis.rotated(cam_basis.x, -cam_basis.get_euler().x)
  19. dir = cam_basis * dir
  20. # Air movement.
  21. apply_central_impulse(dir.normalized() * 0.04)
  22. if on_ground():
  23. # Ground movement (higher acceleration).
  24. apply_central_impulse(dir.normalized() * 0.08)
  25. # Jumping code.
  26. # It's acceptable to set `linear_velocity` here as it's only set once, rather than continuously.
  27. # Vertical speed is set (rather than added) to prevent jumping higher than intended
  28. # if the ShapeCast3D collides for multiple frames.
  29. if Input.is_action_pressed(&"jump"):
  30. linear_velocity.y = 7
  31. # Test if there is a body below the player.
  32. func on_ground():
  33. if shape_cast.is_colliding():
  34. return true
  35. func _on_tcube_body_entered(body):
  36. if body == self:
  37. get_node(^"WinText").show()