【问题标题】:Start C# script when image is detected for Vuforia+Unity为 Vuforia+Unity 检测到图像时启动 C# 脚本
【发布时间】:2020-06-02 13:31:51
【问题描述】:

我写了一个脚本,比如在 C# 中将球体螺旋旋转到中心。但是,脚本甚至在检测到图像目标之前就开始了,当我将图像目标放在相机前面时,脚本已经执行了。

我读到需要为此修改 DefaultTrackableBehaviour 脚本。我看到了一些视频,这些视频展示了如何在检测到图像目标时启动音频,但是当检测到图像目标时如何启动脚本。

这是对象的层次结构

这是我只想在检测到图像目标时为球体执行的脚本。执行时,球体将螺旋到中心 (0,0,0)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class spiralanti : MonoBehaviour
{
    float angles;
    float radiuss;
    float angleSpeed;
    float rSpeed;
    // Start is called before the first frame update
    void Start()
    {
        angles = 0;
        radiuss = 1f;
        angleSpeed = 250f;
        rSpeed = 0.05f;
        angles = Mathf.Max(0, Mathf.PI);
        radiuss = Mathf.Max(0, radiuss);

        //float x = 0;
        // float y = 0;
        // float z = 0;
    }

    // Update is called once per frame
    void Update()
    {
        angles += Time.deltaTime * angleSpeed;
        radiuss -= Time.deltaTime * rSpeed;
        if (radiuss <= 0)
        {
            float x = 0;
            float y = 1;
            float z = 0;

            transform.localPosition = new Vector3(x, y, z);
        }

        else
        {
            float x = radiuss * Mathf.Cos(Mathf.Deg2Rad * angles);
            float z = radiuss * Mathf.Sin(Mathf.Deg2Rad * angles);
            float y = 1;

            transform.localPosition = new Vector3(x, y, z);
        }


        }

 }

请帮忙。

【问题讨论】:

    标签: c# unity3d vuforia


    【解决方案1】:
    1. 在 DefaultTrackableBehaviour 脚本中,创建一个布尔变量 targetFound。
    2. 从脚本的 Start() 方法中复制所有内容并添加到 DefaultTrackableBehaviour 脚本的 start() 方法中。
    3. 将 Update() 方法从脚本复制并添加到 DefaultTrackableBehaviour 脚本
    4. 在 Update() 方法中添加 if 条件来检查 targetFound 是真还是假,如果 targetFound 为真,则执行 Update() 代码。
    5. 在 onTrackingFound() 方法中将 targetFound 值设置为 true。
    public bool targetFound = false;
    
    void onTrackingFound() {
              targetFound = true;
            }
    
    void Update() {
             if (targetFound){
              // your code here
           }
    }
    

    【讨论】:

    • 如何使用 onTrackinFound() 调用脚本?
    • 如果您更新一些代码或您想要实现的目标的屏幕截图,我将能够为您提供更多帮助。
    • 只需创建一个标志,例如targetFound;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    相关资源
    最近更新 更多