0
\$\begingroup\$

I added the cube on the image target of Vuforia. I also added the Vuforia 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.

\$\endgroup\$
0
\$\begingroup\$

The button is behaving just as a normal button would. It is only clicking once so you probably do have a very small change to the y rotation but it is not continuing to rotate over time.

Two ways you can fix. you can use a bool to trigger the method in a update. Something like bool beginRotate.

bool beginRotate = false;

 public void OnButtonPressed(VirtualButtonBehaviour vb){

    Debug.Log ("Button pressed");
    beginRotate = true;
}


void Update(){

if(beginRotate)
cube.transform.Rotate (new Vector3(0,Time.deltaTime*1000,0));

}

Or another way would be to call a COroutine on button press.

 public void OnButtonPressed(VirtualButtonBehaviour vb){

 Debug.Log ("Button pressed");
 StartCoroutine(RotateCube());
}

IEnumerator RotateCube()
{             
    cube.transform.Rotate(new Vector3(0, Time.deltaTime * 1000, 0));
    yield return new WaitForSeconds(0.01f);   
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks.. it fixed the problem. actually the 2 solutions that you mentioned work differently. the first one solution rotate the cube continuously on every frame as you hold down the button. while the second one rotate it once on holding down the button. \$\endgroup\$ – Syed Aun Jul 6 '18 at 9:18
  • \$\begingroup\$ Sorry my reputation is low so i am unable to vote up your answer. \$\endgroup\$ – Syed Aun Jul 6 '18 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.