【发布时间】:2018-07-04 15:09:24
【问题描述】:
我在 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;
}
}
该按钮似乎工作正常,因为onButtonPressed 函数中的Debug.Log ("Button pressed"); 和rend.material.color = Color.blue; 语句工作正常。但是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);
}
}
也看看控制台窗口
【问题讨论】:
标签: c# unity3d augmented-reality vuforia