立方体不会通过在 Unity3d 中按下 Vuforia 虚拟按钮来旋转
The cube does not rotate by pressing Vuforia virtual button in Unity3d
我在 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);
}
}
也看看控制台 window
如果你想在按住按钮时旋转每一帧,但在松开按钮时停止旋转,那么使用布尔变量来实现。在OnButtonPressed
中设置为true
,在OnButtonReleased
中设置为false
。在 Update
函数中检查 true
中是否有此标志,然后旋转立方体。
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));
}
我在 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);
}
}
也看看控制台 window
如果你想在按住按钮时旋转每一帧,但在松开按钮时停止旋转,那么使用布尔变量来实现。在OnButtonPressed
中设置为true
,在OnButtonReleased
中设置为false
。在 Update
函数中检查 true
中是否有此标志,然后旋转立方体。
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));
}