How do you make multiple 3D rigidbodies without duplicating them in the node editor?

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

I currently have a rigidbody set up like a collectable, which deletes itself from the scene once I trigger the collision. It uses a area-enter signals in the area trigger, and a script for the rigidbody itself. Heres the main part of my code that I’m using:

extends RigidBody

var isRotatingFaster

func _ready():
	isRotatingFaster = false

func _process(delta):
	
	if isRotatingFaster == true:
		get_parent().get_node("Item1/GreenItem").rotate_y(7 * delta)
		
	if isRotatingFaster == false:
		get_parent().get_node("Item1/GreenItem").rotate_y(1 * delta)
	
func _on_Area_body_entered( body ):
	if body is KinematicBody:
		get_parent().remove_child(self)

func _on_Area1_body_entered( body ):
	if body is KinematicBody:
		isRotatingFaster = true

func _on_Area1_body_exited( body ):
	if body is KinematicBody:
		isRotatingFaster = false

If I were to duplicate the rigidbody and attach a script with the same code, it would probably work, but if I wanted 50 objects at the same time, I’d have to do exactly what I described 50 times!?!?

While I do understand for loops which is how I would normally solve this problem in a language like java, I’m unsure how to approach this problem, or if there is an alternative way to do it in godot. Basically the goal is to have 50 collectables randomly placed across the play field.

Any help would be appreciated, thanks!

You could reduce performance overhead by just using an Area2D instead of a RigidBody if you don’t need realistic physics for your collectibles. You can rotate an Area2D node.

DodoIta | 2018-03-13 10:03

Hey there thanks for commenting. My project is in 3D which I forgot to mention. The rigid is 3D. I don’t really have realistic physics so I might alter it a bit so I dont utilize something I dont need.

winniethewind | 2018-03-13 11:58

:bust_in_silhouette: Reply From: johannesg

Plenty of ways to implement this. One of them would be to simply save the node as a scene and manually placing 50 of them in the level. This would allow for more hands on experience and allow you to pick exactly where you want these items to be placed.

If you want something more random and automatic, then a for loop would be your way to go. A node in the level with a script attached would act as a item generator. The script would simply have a for loop that would run 50 times and pick a random location at each time, and instance a clone of the collectible item node in that said location.