Add the DragControls to a dynamically created Cube

import {DragControls} from "@needle-tools/engine"; //... const cube = new THREE.Mesh(geometry, material); cube.addNewComponent(DragControls);

shouldn’t the cube now be dragable?

Original Post on Discord

by user 367081956959322114

It needs to have a raycaster component somewhere in the parent hierarchy (for example an ObjectRaycaster) - often times its at the top of the gltf file but you can put it on some sub-object of course if you dont want to raycast against your whole scene
unknown.png

It will raycast against all children basically

You can have multiple of those in your scene

when I say Scene.add(cube); and have the Object Raycaster script on SceneRoot (green circle icon) should be enough right?

by user 367081956959322114

but the cube is not reacting

by user 367081956959322114

The green “SceneRoot” object is not “this.context.scene” - it’s a child

oh so ehm where is the scene? hehe

by user 367081956959322114

What do you mean? this.context.scene is the threejs scene but the ObjectRaycaster is on one of the child objects (you can console.log(this.context.scene) to get a better understand of what I mean - components get unpacked in userData.components)

so I should add the cube to the SceneRoot and not to the scene right?

by user 367081956959322114

ok let me log that

by user 367081956959322114

console.log(this.scene);
console.log(this.context.scene);
aha its the same

by user 367081956959322114

Yes (depending on your setup you can also add it to this.gameObject) - what I usually do is make a reference to the object where I want to child to end up in and then use that (look at the code of the Duplicatable component)
unknown.png

sceneRootglb is where I should put my cube

by user 367081956959322114

You can assign them in Unity
unknown.png

this make sense right!

by user 367081956959322114

Ah yes - this.scene is just a shorthand

the power of having an editor :smile:

(maybe I should read more about the basics) but what does the annotation @serializeable(Object3D) do? is it there to save the session?

by user 367081956959322114

It’s used to resolve the reference you made in Unity to the threejs object when the gltf is loaded