一、导入插件和素材

二、物体被抓取脚本

三、搭建场景

四、贴图

五、碰撞交互

六、添加物理材质


七、控制球的行为

Bowling
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bowling : MonoBehaviour {
//重置位置 公开是为了扳机键按下获取的重置位置 但是部想显示 使用【HideInInspector】
[HideInInspector]
public Vector3 stratPosition;
//为了更好的获取它 静态的 做一个单例模式 在awake方法执行
public static Bowling instance;
void Awake() {
//单例模式条件判断时候为空
if (instance==null)
{
instance = this;
}
}
void Start () {
//赋值
stratPosition = transform.position;
}
void Update () {
//判断球的高度小于一定值的时候 重置位置;
if (transform.position.y<5f)
{
transform.position = stratPosition;
//速度,叫速度也归零
//GetComponent<Rigidbody>().velocity = Vector3.zero;
// GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
Rigidbody rigi=GetComponent<Rigidbody>();
rigi.velocity = Vector3.zero;
rigi.angularVelocity = Vector3.zero;
}
}
}
八、控制瓶子行为

BowlingPins
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BowlingPins : MonoBehaviour {
//定义集合引用瓶子的q起始位置
private List<Vector3> stratPosition=new List<Vector3>();
//定义集合引用transform的信息
private List<Transform> pins=new List<Transform>();
//记录瓶子掉下的数量
private int numPinsDown = 0;
void Start () {
//添加
foreach (Transform child in transform)
{
stratPosition.Add(child.position);
pins.Add(child);
}
}
void Update () {
//遍历瓶子状态
foreach (Transform child in transform)
{
//判断瓶子时候是激活了状态并且高度小于5的时候隐藏瓶子
if (child.gameObject.activeInHierarchy&&child.position.y<5f)
{
//隐藏
child.gameObject.SetActive(false);
//记录掉下去的瓶子
numPinsDown++;
}
//判断掉下的瓶子数量等于初始的瓶子数量时 重置游戏
if (numPinsDown==pins.Count)
{
Reset();
}
}
}
//重置游戏方法
void Reset() {
//遍历子物体重新赋值
for (int i = 0; i < pins.Count; i++)
{
//瓶子设置为可见
pins[i].gameObject.SetActive(true);
//瓶子起始位置
pins[i].position = stratPosition[i];
//无旋转
pins[i].rotation = Quaternion.identity;
//速度和角速度归零
Rigidbody rigi = pins[i].GetComponent<Rigidbody>();
rigi.velocity = Vector3.zero;
rigi.angularVelocity = Vector3.zero;
}
//数量重置;
numPinsDown = 0;
}
}
九、重置游戏

ResetBall
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;
public class ResetBall : MonoBehaviour {
//此脚本挂在VRTK Controller上;
//引用单例
public GameObject _Bowling;
void Start () {
//看场景2的脚本
GetComponent<VRTK_ControllerEvents>().TriggerClicked += new ControllerInteractionEventHandler(DoTriggerClicked);
//找到里面的参数在按f12追踪重写方法;
}
void Update () {
}
//重写以上方法
private void DoTriggerClicked(object sender, ControllerInteractionEventArgs e)
{
//扳机键按下处理的方法
//DebugLogger(e.controllerIndex, "TRIGGER", "clicked", e);
//调用单例模式 重置保龄球位置
_Bowling.transform.position = Bowling.instance.stratPosition;
//速度和角速度重置
// _Bowling.GetComponent<Rigidbody>().velocity = Vector3.zero;
// _Bowling.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
Rigidbody rigi=GetComponent<Rigidbody>();
rigi.velocity = Vector3.zero;
rigi.angularVelocity = Vector3.zero;
}
}
网友评论