首先是新建一个Vrint的脚本,并复制以下的代码!!!
using UnityEngine;
using System.Collections;
using VRStandardAssets.Utils;
using System;
public class Vrint: MonoBehaviour
{
VRInteractiveItem VR;
public bool on;
public event Action onTrigger;
public event Action onOver;
public event Action onOut;
void Awake()
{
VR = GetComponent() ?? gameObject.AddComponent();
}
void OnEnable()
{
VR.OnOver += OnOver;
VR.OnOut += OnOut;
}
void OnDisable()
{
VR.OnOver -= OnOver;
VR.OnOut -= OnOut;
}
void OnOut()
{
on = false;
if (onOut != null)
{ onOut(); }
}
void OnOver()
{
on = true;
if (onOver != null)
{
onOver();
}
}
private void OnTrigger()
{
on = true;
if (onTrigger != null)
{
onTrigger();
}
}
}
保存该脚本,然后新建一个Trigger的一个脚本,并复制以下的代码:
using UnityEngine;
using System.Collections;
using VRStandardAssets.Utils;
public class Trigger: MonoBehaviour {
VRInput input;
public bool ison;
TextMesh te;
chufa cc;
void Awake() {
input = GameObject.Find("VRCamera").GetComponent();
te = GameObject.Find("jiafen").GetComponent();
}
void Start () {
input.OnClick += delegate
{
//cc.on是用来判断用户是否触发了该物体
if (cc.on) {
//判断游戏物体的标签为monkey的就加分
if (base.gameObject.tag == "monkey")
{
hexin.h += 10;
te.text = "得分:" + hexin.h;
}
//判断游戏物体的标签为other的就减分
if (base.gameObject.tag == "other")
{
hexin.h -= 10;
te.text = "得分:" + hexin.h;
}
cc.on = false;
gameObject.SetActive(false);
}
};
}
void OnEnable() {
cc = GetComponent() ?? gameObject.AddComponent();
}
}
注意一定要把Trigger脚本挂在到需要判断的游戏物体上,并把射中需要加分的游戏物体的标签设置为monkey,射中需要减分的游戏物体设置为other。
三和尚 原创文章 23获赞 28访问量 3369 关注 私信 展开阅读全文