Attaching Script to Custom Hands

I’m trying to use custom Hands like the ones in the needleSamples example, and I’m attempting to attach a script to them, but it’s not working.

Hi boss15493!

To attach a script to custom hands in Needle Engine, you must ensure that the component you are using supports the interaction with XR controllers or hands correctly. Here’s a concise approach you can follow:

  1. Ensure Correct Components: Use the XRControllerModel component to instantiate your custom models. Ensure that you assign your custom models for left and right hands appropriately.

  2. Implement the Script:
    In your custom component script, inherit from Behaviour and implement the necessary lifecycle methods (like onXRControllerAdded or onUpdateXR). Inside these methods, add your logic to manipulate the hands.

Here’s an example to get you started:

import { Behaviour, GameObject, XRControllerModel, NeedleXRControllerEventArgs } from "@needle-tools/engine";

export class CustomHandScript extends Behaviour {
    onXRControllerAdded(args: NeedleXRControllerEventArgs) {
        const controller = args.controller;

        // Adding functionality when the controller is added
        console.log(`Controller added: ${controller.side}`);
        
        // You can perform actions such as updating the model or assigning functions
    }

    onUpdateXR(args: NeedleXREventArgs) {
        // Update your hand following here
        const ctrl = args.xr.getController(this.side);
        if (ctrl) {
            // Example of setting position and visibility based on tracking status
            this.gameObject.position.copy(ctrl.gripWorldPosition);
            this.gameObject.visible = ctrl.isTracking;
        }
    }
}
  1. Attach the Script: In your scene, make sure to add this script to the same GameObject where you have the XRControllerModel component.

  2. Check for Errors: If it’s not working, ensure there are no console errors and that your models are correctly referenced.

For more information on how to create and manage custom hands, you may refer to the Needle Engine documentation.

Let me know if you need further assistance or specific details!

3 replies left
thread_nEPSCHknYQ0CcG4JknZa5V8h
Reply to this message to continue the conversation

Can’t I directly attach the script I need to the prefab I’m for the custom hands?

Hi boss15493!

Yes, you can attach a script directly to the prefab for your custom hands. Here’s how to do it effectively:

  1. Open Your Prefab: In your asset folder, locate the prefab for your custom hands and open it in the prefab editor.

  2. Add Script Component: Once you have the prefab open, you can add your custom script component directly to the prefab just like you would to any GameObject:

    • Select the prefab in the hierarchy.
    • Click on “Add Component” and choose your script from the list.
  3. Implement the Functionality: In your script, make sure to implement the functionality needed to interact with the controller model as described earlier. You still need to manage the update logic and any events from the XR controller.

  4. Save and Test: After attaching the script, save the prefab. When you instantiate or reference this prefab in your scene, the script will be included.

  5. Verify Interaction: Make sure your prefab script has access to the necessary components (like XRControllerModel) and events to function correctly during XR interactions.

By following these steps, you can ensure your script operates as needed with the custom hands prefab. Let me know if you encounter any issues or need further clarification!

3 replies left
thread_nEPSCHknYQ0CcG4JknZa5V8h
Reply to this message to continue the conversation

its not working im attaching the script in the prefab


and this is my script

but i dont see nothing in the onUpdateXR

Hi boss15493!

If you’re not seeing anything in the onUpdateXR method, here are a few possible reasons and steps to troubleshoot the issue:

  1. Verify XR Session Status: Check if the XR session is active when your CustomHandScript component is supposed to run. Ensure you’re entering XR mode correctly by calling the session start functions as needed and that the XR controllers are connected.

  2. Controller Reference Check: Make sure the controllers are being correctly referenced in your onUpdateXR method. Add debug logs to verify that the args.xr object is valid and that the controllers are not null. Example:

    console.log("XR Session:", args.xr);
    console.log("Left Controller:", args.xr.leftController);
    console.log("Right Controller:", args.xr.rightController);
    
  3. Script Activation: Make sure that the prefab you modified is correctly instantiated and activated in the scene. If it’s added but not active, it won’t receive updates.

  4. Component Hierarchy: Ensure that the script is attached to the correct GameObject in the hierarchy that is capable of accepting XR events. It should also not be in a disabled state at runtime.

  5. Debugging Lifecycle: You can log into the onBeforeXR, onEnterXR, and onLeaveXR methods to ensure that your component is correctly entering and exiting XR events.

Here is an adjusted version of your update method with logging:

onUpdateXR(args: NeedleXREventArgs): void {
    console.log("Updating XR for hand script");
    const leftController = args.xr.leftController;
    const rightController = args.xr.rightController;

    if (leftController) {
        console.log("Left Controller is active");
        // Your logic for the left controller here
    }

    if (rightController) {
        console.log("Right Controller is active");
        // Your logic for the right controller here
    }
}

Try implementing these checks and adjustments in your code, and feel free to share your findings if it still doesn’t work!

2 replies left
thread_nEPSCHknYQ0CcG4JknZa5V8h
Reply to this message to continue the conversation

Hi, do you have a link to your website with this script?

Hi Marcel, I don’t have any website link in my script. Is it necessary for it to work? I’m using the custom hands as a prefab, just like the one in your example. also i want to add colliders to the custom hands

Hi, no no I just asked so we can perhaps take a look.

Do you see any error in the browser console? For debugging I can recommend the immersive web emulator extension if you don’t use this already: https://chromewebstore.google.com/detail/immersive-web-emulator/cgffilbpcibhmcfbgggfhfolhkfbhmik

No, nothing appears in the console. I’m using my Quest 3 and I can see the custom hands without any problem, but the script doesn’t work . No console.log appears in start, update, onpdateXR, or awake. Similarly, if I try to attach any collider, it doesn’t work either , I can’t see it in ?showColliders. Basically, I want to implement the same custom-logic that I have with the avatar with the leftHand and rightHand, for the custom hands.

and im debugging with the ?console

Hi Marcel,I apologize for the inconvenience, but I’m still encountering the same issue. Are there any additional troubleshooting steps you could suggest? I’m unsure whether the problem lies with my implementation or if it’s a potential bug in Needles.

Hi, it does sound like a bug but we didnt get to address it yet. Please DM me your license information if you do have any so we can prioritize it.

As a workaround you might be able to attach your script to the custom avatar prefab (assigned in the WebXR component) and then watch the children hierarchy to modify your hand model when it gets added

1 Like