"3D" How to spawn an object when player raycast is colliding and player click?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By King C rocodile

for my cooking game im trying to make a dispenser that spawn whatever food when the player raycast and Input.is_action_just_pressed(“use”): is colliding with it.

imagine a square which is the object, spawns a tomato if the player raycast is colliding with it and clicking.
how can i make the spawner object detect the player raycast?
any tips or suggestions?

I’m not sure what you’re asking for help with. Your description sounds reasonable. If a raycast is colliding with an appropriate object and the player clicks, spawn a new item.

What part of that are you having difficulties with?

jgodfrey | 2022-08-22 20:00

oh man i completely messed up my question…

i meant to ask how i can make the object realise the players raycast is colliding with it then if the player press the assigned button “use”, the object spawns a tomato.
my bad!

the spawning i already know how to do, just not the detecting of raycast :slight_smile:

King C rocodile | 2022-08-22 20:25

The object won’t be able to detect the player’s raycast because the raycast will only pass information to the player. Couple of options I can think of are:

a) Get the player to send out a signal when its raycast hits an object (maybe pass the objects as a parameter). Then that object can pick up the signal and dispense the tomato. You could use an if statement to take care of the button press:

func _on_RaycastCollisionDetected(obj: Object) -> void:
    if Input.is_action_just_pressed("use"):
        emit_signal("UseObject", obj)

or b) Just call the relevant function in the Object script. You could do this by replacing the emit_signal line above to obj.DispenseTomato() or whatever the function is called in the Object script.

dacess123 | 2022-08-22 20:59