Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node2D
- #gets player input vector from player parent, used for rotating the grabNode
- var input_vector: Vector2 = Vector2.ZERO
- @onready var parentObject: CharacterBody2D = $".."
- func player_control_vector():
- return parentObject.input_vector
- #handles rotation of grabNode, a node which grabRay and grabLocation is attached to
- @onready var grabRay = %grabRay #raycast2D used for detecting objects to pick up
- func grab_rotate(inputVector):
- if inputVector != Vector2.ZERO:
- set_rotation(inputVector.angle() - PI/2) #rotates grabbing node
- #handles the grabbing nitty gritty
- @onready var grabLocation = %grabLocation #where the grabbed object will be teleported to every frame
- var isHandEmpty: bool = true
- var collider: Object = null
- func grab_object():
- if not isHandEmpty : #if player is carrying something, it is moved to player's front
- collider.global_position = grabLocation.global_position
- if Input.is_action_just_pressed("input_left_click"):
- #drops the carried item
- if not isHandEmpty:
- collider.global_position = grabLocation.global_position
- collider.set_collision_layer(256) #somehow puts the object back to their collision layer
- isHandEmpty = true
- unfreezeNextFrame += 1
- #picking up the stuff
- elif isHandEmpty and grabRay.is_colliding():
- collider = grabRay.get_collider(0) #selects first item to collide with ray
- collider.set_collision_layer(9) #makes the object non-colliding
- isHandEmpty = false
- if collider != null:
- collider.freeze = true
- func _physics_process(_delta: float) -> void:
- grab_rotate(player_control_vector())
- grab_object()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement