#Take items and drop them
82 messages · Page 1 of 1 (latest)
hey :)
i've found a tutorial and kind of made it working...
but i've got one problem:
i cant make it drop the picked item
extends RayCast3D
@onready var hold_pos = $Hold_Position
@onready var is_held = false
@onready var old_pos
@onready var old_rot
func _physics_process(delta):
if self.is_colliding():
var collider = self.get_collider()
var obj_col = collider.get_parent()
#print(obj_col)
if Input.is_action_just_pressed("player_interaction") && is_held == false:
old_pos = obj_col.get_global_transform()
old_rot = obj_col.rotation_degrees
obj_col.get_parent().remove_child(obj_col)
hold_pos.add_child(obj_col)
obj_col.transform.origin = Vector3(0.8,1.2,-1)
obj_col.rotation_degrees = Vector3(0,0,0)
is_held = true
if Input.is_action_just_pressed("player_interaction") && is_held == true:
it is missing the dropping part
yeah, i dont really have an idea how to make it drop the item
the tutorial was just about interacting with objects, and thats why i can only pick up items
and i dont really know how to make it drop..
You simply unparent it from hold_pos
and parent it to the world
like it was before
yeah thats also a problem, how do i $ the root?
(just like @onready var hold_pos = $Hold_Position but for the root)
Either you could store the object's parent when you first start holding it and restoring it later
or use this get_tree().root.get_child(0),
though I prefer the first solution
okay,
if Input.is_action_just_pressed("player_interaction") && is_held == true:
hold_pos.remove_child(obj_col)
get_tree().root.get_child(0).Scene.add_child(obj_col)
is_held = false
just tried this code block, and when i try to pick something up (in this case a cube) it give me the error
btw, this is my file-tree:
This warning just says you are not using the delta you declared
which is 100% fine
whoops
so there probably is another error
Why did you put the .Scene
get_tree().root.get_child(0).add_child(obj_col) is probably fine
okay, now when i try too pick up the cube, it teleports away
wait lemme make a video
cant interact with it anymore after it teleported
maybe its because theres no delay? i mean when i press E on the cube, its instantly picked up, and maybe then its instantly dropped?
i dunno
Yes
You need to add an else
so it doesnt pick up and drop in the same press
because you set held to true so the second if goes through
alright, now i have no error, but cant drop it too
extends RayCast3D
@onready var hold_pos = $Hold_Position
@onready var is_held = false
@onready var old_pos
@onready var old_rot
func _physics_process(delta):
if self.is_colliding():
var collider = self.get_collider()
var obj_col = collider.get_parent()
#print(obj_col)
if Input.is_action_just_pressed("player_interaction") && is_held == false:
old_pos = obj_col.get_global_transform()
old_rot = obj_col.rotation_degrees
obj_col.get_parent().remove_child(obj_col)
hold_pos.add_child(obj_col)
obj_col.transform.origin = Vector3(0.8,1.2,-1)
obj_col.rotation_degrees = Vector3(0,0,0)
is_held = true
else:# Input.is_action_just_pressed("player_interaction") && is_held == true:
hold_pos.remove_child(obj_col)
get_tree().root.get_child(0).add_child(obj_col)
is_held = false
@vague plinth nvm, when i look at the cube i get errors
if Input.is_action_just_pressed("player_interaction"):
if is_held == false:
old_pos = obj_col.get_global_transform()
old_rot = obj_col.rotation_degrees
obj_col.get_parent().remove_child(obj_col)
hold_pos.add_child(obj_col)
obj_col.transform.origin = Vector3(0.8,1.2,-1)
obj_col.rotation_degrees = Vector3(0,0,0)
is_held = true
else:
hold_pos.remove_child(obj_col)
get_tree().root.get_child(0).add_child(obj_col)
is_held = false
Your if structure is wrong, try something more like that
rn you are telling it to drop it everyframe you are not pressing the interaction button and arn't holding anything
so it tries to drop something it doesn't have and goes boom i'm pretty sure
okay, this time there are actually no errors (it dont go boom) and i cant drop it
@vague plinth do you want the project files?
I think we can figure it out without it
Send your code
okay :)
extends RayCast3D
@onready var hold_pos = $Hold_Position
@onready var is_held = false
@onready var old_pos
@onready var old_rot
func _physics_process(delta):
if self.is_colliding():
var collider = self.get_collider()
var obj_col = collider.get_parent()
#print(obj_col)
if Input.is_action_just_pressed("player_interaction"):
if is_held == false:
old_pos = obj_col.get_global_transform()
old_rot = obj_col.rotation_degrees
obj_col.get_parent().remove_child(obj_col)
hold_pos.add_child(obj_col)
obj_col.transform.origin = Vector3(0.8,1.2,-1)
obj_col.rotation_degrees = Vector3(0,0,0)
is_held = true
else:
hold_pos.remove_child(obj_col)
get_tree().root.get_child(0).add_child(obj_col)
is_held = false
Do you know how to inspect the hierarchy remotely when your game is running ?
When the game is running, there is a remote button that allows you to inspect how the hierarchy is looking
So you can see what happens to your held object
Can you put a print inside the else to see if it doesn't reach it for some reason
if Input.is_action_just_pressed("player_interaction"):
if is_held == false:
print("if works!")
old_pos = obj_col.get_global_transform()
old_rot = obj_col.rotation_degrees
obj_col.get_parent().remove_child(obj_col)
hold_pos.add_child(obj_col)
obj_col.transform.origin = Vector3(0.8,1.2,-1)
obj_col.rotation_degrees = Vector3(0,0,0)
is_held = true
else:
print("else works!")
hold_pos.remove_child(obj_col)
get_tree().root.get_child(0).add_child(obj_col)
is_held = false
it only prints if works!
Weird
if Input.is_action_just_pressed("player_interaction"):
print("test")
if is_held == false:
print("if works!")
old_pos = obj_col.get_global_transform()
old_rot = obj_col.rotation_degrees
obj_col.get_parent().remove_child(obj_col)
hold_pos.add_child(obj_col)
obj_col.transform.origin = Vector3(0.8,1.2,-1)
obj_col.rotation_degrees = Vector3(0,0,0)
is_held = true
else:
print("else works!")
hold_pos.remove_child(obj_col)
get_tree().root.get_child(0).add_child(obj_col)
is_held = false
Try seeing if it gets inside the first if
maybe you have something else that I can't see preventing the code from reaching here
test
if works!
--- Debugging process stopped ---
(i tried dropping it and then closed the program)
Can you show the full file ?
extends RayCast3D
@onready var hold_pos = $Hold_Position
@onready var is_held = false
@onready var old_pos
@onready var old_rot
func _physics_process(delta):
if self.is_colliding():
var collider = self.get_collider()
var obj_col = collider.get_parent()
#print(obj_col)
if Input.is_action_just_pressed("player_interaction"):
print("test")
if is_held == false:
print("if works!")
old_pos = obj_col.get_global_transform()
old_rot = obj_col.rotation_degrees
obj_col.get_parent().remove_child(obj_col)
hold_pos.add_child(obj_col)
obj_col.transform.origin = Vector3(0.8,1.2,-1)
obj_col.rotation_degrees = Vector3(0,0,0)
is_held = true
else:
print("else works!")
hold_pos.remove_child(obj_col)
get_tree().root.get_child(0).add_child(obj_col)
is_held = false
@vague plinth my big brain solved the issue (ironic)
it could only drop, when the raycast was colliding with the item!
yeah
thanks!
Nice, happy to help