【问题标题】:The cube does not rotate by pressing Vuforia virtual button in Unity3d在 Unity3d 中按 Vuforia 虚拟按钮不会旋转立方体
【发布时间】: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


    【解决方案1】:

    如果您想在按住按钮时旋转每一帧但在释放时停止,则使用布尔变量来执行此操作。将其设置为OnButtonPressed 中的trueOnButtonReleased 中的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));
    }
    

    【讨论】:

    • 我尝试了您的解决方案,但它对我不起作用。对不起
    • 同样的解决方案适用于简单的 UI 按钮,但不适用于 Vuforia 虚拟按钮。
    • 它怎么可能适用于 UI 按钮而不适用于虚拟按钮?你确定这些函数都被调用了吗?
    • 我只是在 UI 按钮上使用了布尔变量和 transform.Rotate() 的逻辑,它在那里工作正常......
    • 它应该与OnButtonPressed 一起使用,您能否在我的回答中仅使用OnButtonPressedOnButtonReleased,然后上传控制台日志选项卡的屏幕截图。我想看日志。
    猜你喜欢
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 2019-09-20
    • 1970-01-01
    相关资源
    最近更新 更多