Problem with item in Godot3D. The game "Butterfly Kindergarten"

Godot Version

Godot Engine 3D 4.6

Question

I’m creating a very interesting horror game about a boy who survives in a kindergarten and escapes from creepy toys. Some of them act as staff, replacing humans, while others are ordinary toys.

When I launch the map, the first item I need to get, the flashlight, just doesn’t work. I’ve written scripts based on the tutorials, trying to make sure the flashlight can be picked up, but when I try, there’s no indication of the click button, and the object itself can’t be picked up.

I made a flashlight by hand in a 3D blender

This is a character with a RayCast 3D beam.

Now the scripts

This is a flashlight script

extends SpotLight3D

var picked_up = false

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if Input.is_action_just_pressed("flashlight") && picked_up == true:
		visible = !visible
		$toggie.play()
extends Node3D

var flashlight

func _ready():
	flashlight = get_node("/root/" + get_tree().current_scene.name + "/player/head/flashlight")

func interact():
	flashlight.picked_up = true
	queue_free()

This is a flashlight pickup

extends RayCast3D

var int_text

func _ready():
	int_text = get_node("/root/" + get_tree().current_scene.name + "/UI/interact_text")

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if is_colliding():
		var hit = get_collider()
		if hit.has_method("interact"):
			int_text.visible = true
			if Input.is_action_just_pressed("interact"):
				hit.interact()
		else:
			int_text.visible = false
	else:
			int_text.visible = false

This is a raycast

If you see that I made any mistakes or you need more screenshots, I can send them to you from below. This is very important to me because otherwise I can’t continue with the next stages of the video tutorials.

By the way, this will indicate the action button that I can’t see.

Put a print statement inside if is_colliding(): block to see if the raycast is working.

1 Like

I also thought about this at first because the “print” command looks more convenient than without it.

  • By the way, where should I insert it? When I put it in brackets, they turn red.
if is_colliding():
	print("The raycast is colliding!")
	# the rest of your stuff

If it doesn’t print anything, the ray setup is likely not correct. You can try elongating the ray for start.

1 Like

When I extended the ray beam, I realized that perhaps it wasn’t the ray beam that was responsible, but the object itself, which for some reason didn’t obey it.

Does it print or not?

There is no print, or it should be invisible. In any case, the action button designation did not appear on top, as well as the object itself is not selected.

Forget about the object for now. You first need to determine if the raycast itself is colliding. Put a print statement in that block as I suggested and look into the engine’s output window if it prints that phrase when the camera is near and aimed at area.

Just in the output window, if I hope I understood correctly, I noticed that it says “Raycast is colliding”, Apparently this is an infinite number of times when I came and left the object, or maybe something else

This meant that the reaycast itself is colliding properly. Your code that reacts to that collision may have bugs though.

First, get_collider() will return the Area3D node the the ray hit. If a script that has the interact() method is not attached to that node, nothing will happen because your script expect that. So you likely need to get the parent of that are node, if that’s where interact() is.

The interesting thing is that I’ve already had the interact code once, and it seems to be under the general colliding arrow.

I’m a newcomer in godot 3D myself, and Butterfly Kindengarten is actually my very first game that I make, which is why I can sometimes make mistakes in the code, although I try to find solutions to fix them.

Show the flashlight scene structure.

This is flashlight structure

No, it isn’t. We need to know what’s underneath this node.

Raycasts hit area nodes. You need to show where the area node that the raycast is supposed to hit is located in your scene.

The most suitable part of the flashlight looks like this long cylindrical part. Since it is suitable for the raycast beam, however, if the flashlight is in an inverted position facing the player, it will be more difficult to determine the end of the raycast beam, so it may be better to choose the top of the flashlight.

No, I decided that it would be turned off at the end of the flashlight.