赞
踩
在进行虚拟现实技术工具包(VRTK)插件的基本操作之前,首先需要对Unity进行适当的配置。我的运行环境基于SteamVR,因此我选择使用Unity 2019.4.28f1c1版本,这是因为该版本与SteamVR的兼容性较好。
插件包导入1.2.3版本的steamvr Plugin
vrtk v4 Tilia Package Importer
插件链接可以在这里面找到Unity 资源商店 - 优质3D 2D游戏制作资源平台
VRTK官方提供了许多功能丰富的预制体,这些预制体能够帮助我们快速实现各种VR交互效果。
为了导入这些预制体,我通过Unity的菜单栏,选择Window>Tilia>Package Importer进行打开。
关于这些预制体的具体使用方法,可以参考Extend Reality Ltd - Tilia Packages (vrtk.io)
在场景中配置好人物基本操作后,配置场景中的手枪实现抓取使用手枪发射子弹
我在手枪模型上添加了可交互的预制体
并在枪口处加上一个空物体作为子弹发射口。然后,我编写了一段代码,使得手枪在按下特定键位时发射子弹并播放枪声,同时还有枪的动画运动。
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class ma_zidan : MonoBehaviour
- {
- public GameObject bulletPrefab; // 子弹预制体
- public GameObject bulletPrefab2; // 声音预制体
- public GameObject danxia; // 声音预制体
- public GameObject gun_animator;
- public GameObject bulletCountObject; // 显示子弹数量的物体
- private TextMesh bulletCountText; // 显示子弹数量的TextMesh组件
- private Animator animator;
-
- private float shootInterval = 0.2f; // 射击间隔时间
- private float lastShootTime; // 上一次射击的时间
- private int bulletCount = 12; // 子弹数量
-
- void Start()
- {
- if (bulletCountObject != null)
- {
- bulletCountText = bulletCountObject.GetComponent<TextMesh>();
- if (bulletCountText != null)
- {
- UpdateBulletCountText();
- }
- }
- }
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.JoystickButton15) && Time.time - lastShootTime >= shootInterval)
- {
- if (bulletCount > 0)
- {
- Shoot();
- lastShootTime = Time.time;
- bulletCount--;
- UpdateBulletCountText();
- }
- if (bulletCount == 0)
- {
- Instantiate(danxia, transform.position, transform.rotation); // 生成预制体
- bulletCount = 12;
- }
- }
- }
- void Shoot()
- {
- animator = gun_animator.GetComponent<Animator>();
- if (animator != null)
- {
- // 修改Animator的参数
- animator.SetTrigger("shoot"); // 触发Trigger类型的参数
- }
-
- Instantiate(bulletPrefab, transform.position, transform.rotation); // 生成预制体
- Instantiate(bulletPrefab2, transform.position, transform.rotation); // 生成预制体
- }
- void UpdateBulletCountText()
- {
- if (bulletCountText != null)
- {
- bulletCountText.text = bulletCount.ToString();
- }
- }
- }

我使用Unity自带的寻路系统烘培出了蜘蛛可以行走的地形
并创建了动画控制器来控制蜘蛛的动画。
然后,我在蜘蛛上面创建了一个片面作为血条,在代码中通过缩放这个片面来表现蜘蛛的生命值变化。
以下是我的代码
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using static SteamVR_Events;
- using UnityEngine.AI;
- public class HP : MonoBehaviour
- {
- private double liang = 0.00253544;
- public GameObject hp_plan;
- public GameObject zhizhu;
- private NavMeshAgent agent;
- public int maxHealth = 100; // 最大生命值
- public int currentHealth; // 当前生命值
- private Animator animator;
- private Rigidbody rb;
- private void Start()
- {
-
- currentHealth = maxHealth; // 初始化当前生命值为最大生命值
- animator = GetComponent<Animator>();
- agent = GetComponent<NavMeshAgent>();
- rb = GetComponent<Rigidbody>();
- }
-
- // 受到伤害
- public void TakeDamage(int damage)
- {
-
- currentHealth -= damage;
- if (currentHealth <= 0)
- {
- CapsuleCollider capsule = GetComponent<CapsuleCollider>();
-
- // 如果找到了CapsuleCollider组件,则将其删除
- if (capsule != null)
- {
- Destroy(capsule);
- }
-
-
- Vector3 scale = hp_plan.transform.localScale;
- scale.x = 0;//血量缩放为0
- hp_plan.transform.localScale = scale;
- Die();
- }
- else
- {
- Vector3 scale = hp_plan.transform.localScale;
- scale.x = (float)(scale.x - damage * liang);
- hp_plan.transform.localScale = scale;
- // 受伤时进行旋转
- zhizhu.transform.Rotate(-29f, 0f, 0f);
- }
-
-
- }
-
- // 死亡
- private void Die()
- {
- if (animator != null)
- {
- // 修改Animator的参数
- animator.SetTrigger("isdie"); // 触发Trigger类型的参数
- }
- if (agent != null)
- {
-
- agent.isStopped = true;
- agent.velocity = Vector3.zero; // 立即停止所有运动
- rb.isKinematic = true; // 让Rigidbody不受物理力的影响
- //Destroy(navToDelete);
- }
- xunlu xunluScript = GetComponent<xunlu>();
- if (xunluScript != null)
- {
- // 删除物体上的XunLu脚本组件
- Destroy(xunluScript);
- }
- // 处理死亡逻辑,例如播放死亡动画、移除游戏对象等
- Destroy(gameObject,7f);
- }
-
- // Update is called once per frame
- void Update()
- {
-
- }
- }

在这个代码中我设置了蜘蛛被打3枪死亡的同时立刻死亡并停止所有动画运动
虚拟vr作业
在完成以上步骤后,我成功实现了一个打蜘蛛的VR游戏项目,其中包括了手枪的抓取、射击、蜘蛛的动画和生命值变化等多种交互效果。我将这个项目的演示视频上传到了网上,供大家参考和学习。
以上就是我在使用Unity和VRTK进行VR游戏开发过程中的一些经验和技巧,希望能对你有所帮助。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。