Advertisement
Guest User

Untitled

a guest
Sep 7th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. extends Node2D
  2.  
  3. #gets player input vector from player parent, used for rotating the grabNode
  4. var input_vector: Vector2 = Vector2.ZERO
  5. @onready var parentObject: CharacterBody2D = $".."
  6. func player_control_vector():
  7. return parentObject.input_vector
  8.  
  9. #handles rotation of grabNode, a node which grabRay and grabLocation is attached to
  10. @onready var grabRay = %grabRay #raycast2D used for detecting objects to pick up
  11. func grab_rotate(inputVector):
  12.  
  13. if inputVector != Vector2.ZERO:
  14. set_rotation(inputVector.angle() - PI/2) #rotates grabbing node
  15.  
  16. #handles the grabbing nitty gritty
  17. @onready var grabLocation = %grabLocation #where the grabbed object will be teleported to every frame
  18. var isHandEmpty: bool = true
  19. var collider: Object = null
  20. func grab_object():
  21.  
  22. if not isHandEmpty : #if player is carrying something, it is moved to player's front
  23. collider.global_position = grabLocation.global_position
  24.  
  25. if Input.is_action_just_pressed("input_left_click"):
  26.  
  27. #drops the carried item
  28. if not isHandEmpty:
  29. collider.global_position = grabLocation.global_position
  30. collider.set_collision_layer(256) #somehow puts the object back to their collision layer
  31. isHandEmpty = true
  32. unfreezeNextFrame += 1
  33.  
  34. #picking up the stuff
  35. elif isHandEmpty and grabRay.is_colliding():
  36. collider = grabRay.get_collider(0) #selects first item to collide with ray
  37. collider.set_collision_layer(9) #makes the object non-colliding
  38. isHandEmpty = false
  39. if collider != null:
  40. collider.freeze = true
  41.  
  42. func _physics_process(_delta: float) -> void:
  43. grab_rotate(player_control_vector())
  44. grab_object()
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement