Log in or register to post comments

Virtual button trigger animation

June 29, 2012 - 7:49am #1

Hello all,

 

I have a question about virtual buttons. I want to start an animation on my cube object.

Now I have this script:

using UnityEngine;

using System.Collections;

 

public class VirtualButtonHandler : MonoBehaviour, IVirtualButtonEventHandler{

 

    private GameObject cube;

 

// Use this for initialization

void Start () {

// Register with the virtual buttons TrackableBehaviour

        VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();

        for (int i = 0; i < vbs.Length; ++i)

        {

            vbs[i].RegisterEventHandler(this);

        }

 

        // Get handle to the teapot object

        cube = GameObject.Find("cube").gameObject;

 

}

 

    // Called when the virtual button has just been pressed:

    public void OnButtonPressed(VirtualButtonBehaviour vb)

    {

        Debug.Log("OnButtonPressed");

 

        // Add the material corresponding to this virtual button

        // to the active material list:

        switch (vb.VirtualButtonName)

        {

            case "green":

                cube.animation.Play("Rotate180Degrees");

cube.animation.wrapMode = WrapMode.Once;

            break;

        }

    }

 

 

    // Called when the virtual button has just been released:

    public void OnButtonReleased(VirtualButtonBehaviour vb)

    {

 

        // Remove the material corresponding to this virtual button

        // from the active material list:

        switch (vb.VirtualButtonName)

        {

            case "green":

                //mActiveMaterials.Remove(m_TeapotMaterials[3]);

                break;

        }

    }

}

 

And dragged it onto the virtual button.

When I test the app the cube is displayed but the animation is not triggered when I push the virtual button.

Did I forget something or is the script not correct?

 

Thanks in advise.

Virtual button trigger animation

July 10, 2012 - 6:00am #10

If you look at the image below you see that I named the button in the inspector pane on two places at the top and at Virtual Button Behaviour (Script). That's important to get the button to work.

https://docs.google.com/file/d/0B-YKBtylPP4pdTJMd19LV2ZGM1k/edit

Virtual button trigger animation

July 9, 2012 - 6:15pm #9

I dont know what are you talking about the mono behiavour.... i compiled 4 or 5 proyects using just animations but never with VB. 

the proyect runs but the button doesnt do anything, reimported everything but still the same notice... here are some specs:

 

Unity version: 3.4.2f3

Scripts in ImageTarget:

ImageTargetBehiavour

TurnOffBehiavor

DefaultTrackableEventHandler

VirtualButtonEventHandler

 

Scripts in green:

VirtualButtonBehiavour

TurnOffbehiavour

 

Plz Help!

 

Thnks

Virtual button trigger animation

July 9, 2012 - 5:39pm #8

Had you once used a version of this script that did not derive from MonoBehaviour, or another script on ImageTargets that did not derive from MonoBehaviour?

That error is associated w/ a known Unity bug.

see: https://fogbugz.unity3d.com/default.asp?402166_um9ahlcplpdpe9bv

&

http://forum.unity3d.com/threads/68433-A-strange-error-creating-builds

You might try re-importing the project..

Assets > Reimport All

Virtual button trigger animation

July 9, 2012 - 5:20pm #7

the button is called green everywere and my structure is: 

-AR camera

-directional light

-imagetarget (virtualbuttoneventhandler)

 --element

 --Vbutton

it seems to be that the script is not correctly loading: 


Script attached to 'ImageTarget' in scene 'Temp/__BuildPlayer Backupscene' is missing or no valid script is attached.

UnityEditor.HostView:OnGUI()

 

Any ideas?

 

thnks


 

 

Virtual button trigger animation

July 9, 2012 - 9:13am #6

Yes confirm that the name is defined and also that your VB event handler is attached to the parent ImageTarget  - not the VB itself.

You'll also want to check within the event handler to confirm that the names correspond.

e.g.

Debug.Log("OnButtonPressed for "+ vb.VirtualButtonName );

Virtual button trigger animation

July 8, 2012 - 11:55pm #5

Hi Adanete,

Did you named the button in the inspector pane green also? It took me a while before I saw that.

Virtual button trigger animation

July 7, 2012 - 10:22am #4

hey, i copied the script that you posted, attached to the image target, then added the virtual button called green. I changed the name of the animation "rotate180degrees" for "floor2" that was the name of the splitted animation btwn the frame 20 to 30 and nothing happend, is there something im doing wrong.. this is my code.

using UnityEngine;

 

using System.Collections;

 

 

 

public class VirtualButtonHandler : MonoBehaviour, IVirtualButtonEventHandler{

 

 

 

    private GameObject cube;

 

 

 

// Use this for initialization

 

void Start () {

 

// Register with the virtual buttons TrackableBehaviour

 

        VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();

 

        for (int i = 0; i < vbs.Length; ++i)

 

        {

 

            vbs[i].RegisterEventHandler(this);

 

        }

 

 

 

        // Get handle to the teapot object

 

        cube = GameObject.Find("AnimatedBedroom").gameObject;

 

 

 

}

 

 

 

    // Called when the virtual button has just been pressed:

 

    public void OnButtonPressed(VirtualButtonBehaviour vb)

 

    {

 

        Debug.Log("OnButtonPressed");

 

 

 

        // Add the material corresponding to this virtual button

 

        // to the active material list:

 

        switch (vb.VirtualButtonName)

 

        {

 

            case "green":

 

                cube.animation.Play("floor2");

 

cube.animation.wrapMode = WrapMode.Once;

 

            break;

 

        }

 

    }

 

 

 

 

 

    // Called when the virtual button has just been released:

 

    public void OnButtonReleased(VirtualButtonBehaviour vb)

 

    {

 

 

 

        // Remove the material corresponding to this virtual button

 

        // from the active material list:

 

        switch (vb.VirtualButtonName)

 

        {

 

            case "green":

 

                cube.animation.Play("floor1");

 

cube.animation.wrapMode = WrapMode.Once;

 

                break;

 

        }

 

    }

 

}

Virtual button trigger animation

July 2, 2012 - 3:08am #3

That works DavidBeard thank you!

Virtual button trigger animation

June 29, 2012 - 9:52am #2

Attach the event handler to the VB's parent ImageTarget rather than the VB itself.

Log in or register to post comments