当检测到Vuforia+Unity的图像时,启动C#脚本。

问题描述 投票:0回答:1

我写了一个脚本,比如说用C#语言将一个球体旋转到中心。然而,该脚本甚至在检测到图像目标之前就开始了,当我将图像目标放在摄像机前时,该脚本已经执行了。

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

这是对象的层次结构 enter image description here

这是我想在检测到图像目标时才对球体执行的脚本。在执行时,球体将以螺旋的方式到达中心点(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
投票
  1. 在DefaultTrackableBehaviour脚本中,创建一个bool变量targetFound。
  2. 复制你的脚本的Start()方法中的所有内容,并添加到DefaultTrackableBehaviour脚本的start()方法中。
  3. 复制并添加DefaultTrackableBehaviour脚本的Update()方法。
  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
       }
}
© www.soinside.com 2019 - 2024. All rights reserved.