Куб не вращается при нажатии виртуальной кнопки Vuforia в Unity3d - PullRequest
0 голосов
/ 04 июля 2018

Я добавил куб на изображение цели Vuforia. Я также добавил виртуальную кнопку на изображение цели. Теперь я хочу вращать куб, нажимая виртуальную кнопку. Для этого я реализовал следующий скрипт.

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;
    }       

}

Кнопка работает, потому что операторы Debug.Log ("Button pressed"); и rend.material.color = Color.blue; в функции onButtonPressed работают нормально. Но cube.transform.Rotate (new Vector3(0,Time.deltaTime*1000,0)); для вращающегося куба не работает.

Простым является то, что кнопка может изменить цвет куба, но она не вращает куб.

Так что вопрос Как бы я повернул куб, нажав виртуальную кнопку vuforia.

Вопрос обновлен:

Я также попробовал следующий код, но куб все еще не вращается при нажатии кнопки.

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);
    }



}

Также посмотрите на окно консоли

enter image description here

1 Ответ

0 голосов
/ 04 июля 2018

Если вы хотите вращать каждый кадр, пока кнопка удерживается, но останавливаться при отпускании, используйте для этого логическую переменную. Установите true в OnButtonPressed и false в OnButtonReleased. Проверьте, установлен ли этот флаг в true в функции Update, затем поверните куб.

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));
}
...