Search code examples
c#unity-game-engineaugmented-realityvuforia

The cube does not rotate by pressing Vuforia virtual button in Unity3d


I added the cube on the image target of Vuforia. I also added the virtual button on the image target. Now I want to rotate the cube by pressing virtual button. For this purpose I implemented the following script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;

public class rotate : MonoBehaviour, IVirtualButtonEventHandler {

    public GameObject vbtn;
    public GameObject cube;
    public Renderer rend;

    // Use this for initialization
    void Start () {

        vbtn = GameObject.Find ("virtualbtn5");
        vbtn.GetComponent<VirtualButtonBehaviour> ().RegisterEventHandler (this);
        cube = GameObject.Find ("Cube");
        rend = cube.GetComponent<Renderer>();   
    }

    public void OnButtonPressed(VirtualButtonBehaviour vb){

        Debug.Log ("Button pressed");
        cube.transform.Rotate (new Vector3(0,Time.deltaTime*1000,0));
        rend.material.color = Color.blue;

    }

    public void OnButtonReleased(VirtualButtonBehaviour vb){

        Debug.Log ("Button released");
        rend.material.color = Color.red;
    }       

}

The button seems to be working because the Debug.Log ("Button pressed"); and rend.material.color = Color.blue; statements in onButtonPressed function are working fine. But cube.transform.Rotate (new Vector3(0,Time.deltaTime*1000,0)); for rotating cube does not work.

Simple is that the button can change the color of the cube but it does not rotate the cube.

So the question is How would I rotate the cube by pressing virtual button of vuforia.

Question Updated:

I also tried the following code but still the cube does not rotate on button press.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;

public class rotate : MonoBehaviour, IVirtualButtonEventHandler {

    public GameObject vbtn;
    public GameObject cube;
    public Renderer rend;
    public bool rotateit;
    public float speed;
    // Use this for initialization
    void Start () {

        vbtn = GameObject.Find ("virtualbtn5");
        vbtn.GetComponent<VirtualButtonBehaviour> ().RegisterEventHandler (this);
        cube = GameObject.Find ("Cube");
        speed = 100f;

        rend = cube.GetComponent<Renderer>();
        rotateit = false;


    }


    void Update(){


        if (rotateit) {

            cube.transform.Rotate(new Vector3(0, Time.deltaTime * speed, 0));
        }


    }


    public void OnButtonPressed(VirtualButtonBehaviour vb){

        //Debug.Log ("Button pressed");
        //cube.transform.RotateAround(cube.transform.position, new Vector3(0, 1, 0), 10000f * Time.deltaTime);
        //cube.transform.Rotate (new Vector3(0,Time.deltaTime*1000,0));
        rend.material.color = Color.blue;
        rotateit = true;
        Debug.Log ("Button pressed "+rotateit);

    }

    public void OnButtonReleased(VirtualButtonBehaviour vb){

        //Debug.Log ("Button released");
        rend.material.color = Color.red;
        rotateit = false;
        Debug.Log ("Button released "+rotateit);
    }



}

Also have a look at console window

enter image description here


Solution

  • If you want to rotate every frame while the button is held down but stop when released then use a boolean variable to do that. Set it to true in OnButtonPressed and false in OnButtonReleased. Check if this flag in true in the Update function then rotate the cube.

    public GameObject vbtn;
    public GameObject cube;
    public Renderer rend;
    bool pressed = false;
    public float speed = 100f;
    
    // Use this for initialization
    void Start()
    {
    
        vbtn = GameObject.Find("virtualbtn5");
        vbtn.GetComponent<VirtualButtonBehaviour>().RegisterEventHandler(this);
        cube = GameObject.Find("Cube");
        rend = cube.GetComponent<Renderer>();
    }
    
    public void OnButtonPressed(VirtualButtonBehaviour vb)
    {
    
        Debug.Log("Button pressed");
        pressed = true;
        rend.material.color = Color.blue;
    
    }
    
    public void OnButtonReleased(VirtualButtonBehaviour vb)
    {
    
        Debug.Log("Button released");
        pressed = false;
        rend.material.color = Color.red;
    }
    
    void Update()
    {
        if (pressed)
            cube.transform.Rotate(new Vector3(0, Time.deltaTime * speed, 0));
    }