我在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);
}
}
还可以查看控制台窗口
答案 0 :(得分:2)
如果要在按住按钮的同时旋转每一帧,但在释放按钮时停止旋转,则可以使用布尔变量来做到这一点。将其设置为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));
}