mcode
mcode

Reputation: 466

Change Object parent in Three.js?

Is it possible to change the parent of any object?

For example:

scene.add(Object);
Object.Add(OtherObject);
OtherObject.parent = scene; // <-- Something like this

I just know that I can remove the Object and create it again in the new parent, but I'll hope there's another way. I got some Object that are on a plane, if I click them I want fix them to the camera so I can see all the time in front of the camera. And If I click them Again I wanna set the parent again to the plane.

Why did this dont work?

var obj = clicked.clone();
camera.add(obj);
obj.position.set(0,0,-20);

Upvotes: 6

Views: 6666

Answers (2)

shieldgenerator7
shieldgenerator7

Reputation: 1736

This worked for me

parent.attach(child);

The parent is the mesh that you want to parent it to, and the child is the mesh thats going to get parented.

You can do this with or without adding the child to the scene first.

Credit: @pythonjsgeo (im just putting this here as an actual answer so its easier to see) Change Object parent in Three.js?

Upvotes: 1

Jos Dirksen
Jos Dirksen

Reputation: 1903

Easiest way is to use the detach and attach functions of the THREE.SceneUtils object:

THREE.SceneUtils.detach( child, parent, scene );
THREE.SceneUtils.attach( child, scene, parent );

Upvotes: 5

Related Questions