cancel
Showing results for 
Search instead for 
Did you mean: 

How to turn a Mesh into Controller?

Mariyan
Honored Guest

Hi everyone,

I am trying to find a way, after grabbing a Mesh(.glb) to make him replace my controller .

I would be happy for any tips.

 

3 REPLIES 3

NotBadB
Meta Employee

Hi, here's some tips you might find helpful:

After detecting that you have grabbed a certain Mesh, you can take the entity the mesh represents and set its TransformParent to be the right controller entity so that the Mesh is now placed at the same place as the right controller. Note that you will also need to reset the Transform position as well so that it is not placed at an offset from the controller.

You can then hide your controllers by using the setShowControllers() API from the AvatarSystem.

Here's a code snippet that follows these steps:

// Get the right controller. You can also get the left controller the same way
val rightController =
        Query.where { has(AvatarAttachment.id) }
            .eval()
            .filter { it.getComponent<AvatarAttachment>().type == "right_controller" }
            .first()
// sets the grabbed entity to be at the same position as the right controller
entityGrabbed.setComponents(TransformParent(rightController), Transform())

// Turns off visibility of controllers
systemManager.findSystem<AvatarSystem>().setShowControllers(false)

 

Mariyan
Honored Guest

Thank you for the provided solution.

It seems to replace my controller, but it does it without my interaction. Normally I have to be able to first grab the mesh and then it should replace my controller. Also the position that the mesh gets is a bit odd and I am not sure how to adjust it.

 

The whole idea of my app is to use fire extinguisher to extinguish a fire. So a few following questions appear aswell. 😆

 

How could my fire extinguisher spray against the fire and how the fire could then get smaller and then disappear? 

The fire is placed using the meta spatial editor, but for some reason, the animation isn't functioning.

I'm sorry if I'm asking too many questions, but I can't find enough material to learn from.

I would be happy if you could provide me with some kind of course since the youtube videos weren't enough for a person who doesn't have enough experience with android programming.

 

Thank you!

Sounds like a cool app! 

The solution I provided was for after you've grabbed the object. If you want to detect some input on an object, I recommend adding an event listener. Here's an example of how it might be done:

fireExtinguisherEntity 
        .registerEventListener(ButtonDownEventArgs.EVENT_NAME) {
            entity: Entity,
            eventArgs: ButtonDownEventArgs ->
          if (eventArgs.button == ControllerButton.RightSqueeze) {
            // Get the right controller. You can also get the left controller the same way
            val rightController =
               Query.where { has(AvatarAttachment.id) }
               .eval()
               .filter { it.getComponent<AvatarAttachment>().type == "right_controller" }
               .first()
           // sets the grabbed entity to be at the same position as the right controller
           entityGrabbed.setComponents(TransformParent(rightController), Transform())

           // Turns off visibility of controllers
           systemManager.findSystem<AvatarSystem>().setShowControllers(false)

          }
        }



I think our samples on github are very helpful for examples when building apps, if you're looking for more content to learn from. Here's an example I found for adding an event listener in our Animations sample app. There should also be examples in that sample app for using animated assets added via Spatial Editor.