Godot Version
4.2.2
Question
Hello all,
I am trying to recreate something I did in Unity years ago in 2D and doing that requires that my character pick up an item and then drop/throw it. The first part through is where I am having the problem right now picking up the item.
The idea is this press the “P” button on the keyboard and if the canPickUp var is true on the player and you are next to it then pick it up. By doing so the the item is magically given the global co-ordinates of the player so it will appear in the player.
Now I found a tut that did just this, but, it was for Godot3 I think and it wasn’t long before it didn’t work so I tried modyfying what I could to get it working, but, kept getting weird (I before E except after weird) results. And I have two bombs in the game scene.
So the object to pickup is designed like this:
Why we needed 2 collisions shapes has me confused probably because the first one is a for rigidbody2D, but lost me straightaway.
The only thing added to the player script was this:
#var to pick up bomb
var canPickUp = true;
#---------------------
And this is the Object code in it’s entirety:
extends RigidBody2D
#get access to player (hero)
@onready var player = $"../Player"
@onready var area = $Area2D
#vars
var picked = false;
func _physics_process(delta):
#deviated from tut getting the coordintes for the player sprite
#and not a positional node like he did
if(picked == true):
print("picked is true");
var posX = player.global_position.x;
var posY = player.global_position.y;
var posVector = Vector2(posX,posY)
self.global_position = posVector;
print (posVector);
#pass
#to pickup the bomb
func _input(event):
#pickUp action
if(Input.is_action_just_pressed("pickUp")):
var bodies = area.get_overlapping_bodies();
for body in bodies:
print(body);
if( (body.name == "Player") and (player.canPickUp == true) ):
print("...conditions true...");
picked = true;
player.canPickUp = false;
Now, I know that the bodies are being printed but they aren’t correct as the bodies of both “bombs” (objectToGrabX - don’t ask) are being printed out and they can’t possibly both be considered overlapping they aren’t anywhere near each other. For instance pushing the “P” next to one of the bombs give me this in the log window:
ObjectToGrab1:<RigidBody2D#42983229133>
TileMap:<TileMap#39929775354>
ObjectToGrab2:<RigidBody2D#42899343048>
TileMap:<TileMap#39929775354>
And this line:
if( (body.name == “Player”) and (player.canPickUp == true) ):
Isn’t functioning either as words “conditions true” never gets printed and the physics_process that relies on picked equalling true never gets processed either. In fact, I don’t understand the whole body equalling Player is all about.
So, I am a bit stuck on the code and can’t see a solution.
As always, if anyone can help here is a big “thankyou” in advance.
Regards.