Godot V4.2
Can anyone Eli5, because that’s how old i feel right now, why this isn’t working?
I have a characterbody2D (my player) and a rigidbody2d (a spear), that I would like the player to pick up and throw whenever they feel like it. So far I have been able to have them meet in game and I know they can interact with each other due to some print statements I have, but I cant actually pick the spear an add it to my character.
I have 4 scenes. My level, my player, the floor and the spear (which is in a group named spear and the area2d on the spear is in a group spear_area).
The only script I have is on my character. Which has the basic movement godot gives, my detection area2d, and my pick input. My code should be in bold.
extends CharacterBody2D
var able_to_pick = false
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
func _on_detection_area_entered(area):
if area.is_in_group(“spear_area”):
able_to_pick = true
print(“hi”)
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
if Input.is_action_just_pressed(“pick”) and able_to_pick == true:
var spear = get_tree().get_nodes_in_group(“spear”)
spear.position = $Marker2D
add_child(spear)
print(spear)
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()