The project intends to build a virtual reality fantasy world in which you can experience a real-time fight with the AI cute monster. Based on the Oculus Quest Pro and Oculus Integration SDK with Unity, it allows the player to grab the weapon with their own hands in real time. At the same time, the player can walk in the world to experience the detailed environment.
The implementation of the ”VR Sword Adventure” project is a comprehensive process that involves several key components to integrate Oculus VR technology into an immersive gaming experience. VR Tracking: Robust tracking capabilities for both headsets and controllers, providing accurate and responsive movement within the VR environment. Input Management: Tools for handling input from Oculus Touch controllers, including button presses, joystick movement, and hand tracking. Teleportation and Locomotion: Features for implementing movement within the VR world, including teleportation mechanics which are essential for comfortable navigation in VR.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DealDamage : MonoBehaviour
{
public int basedamage = 1;
// Start is called before the first frame update
private void OnTriggerEnter(Collider other){
if (other.CompareTag("Enemy")){
EggEnemy enemy = other.GetComponent<EggEnemy>();
if (enemy != null){
int damage = total_damage();
enemy.TakeDamage(damage);
}
}
if (other.CompareTag("Player")) {
player player = other.GetComponent<player>();
if (player != null){
int damage = basedamage;
player.TakeDamage(damage);
}
}
}
private int total_damage(){
float averagescale = (transform.localScale.x + transform.localScale.y + transform.localScale.z) / 3.0f;
return Mathf.RoundToInt(basedamage * averagescale);
}
// Update is called once per frame
}
Both the weapons and enemies in “VR Sword Adventure” incorporate a blend of components. Grabbable and Handgrabinteractable are derived from Oculus’ SDK, while Deal Damage is custom-designed to implement the logic while the player collides with the enemy. Both enemies and weapons inflict damage on each other using the same script. They differentiate themselves during collisions by employing unique tags, allowing for distinct interactions and responses within the game environment. This approach ensures a cohesive yet versatile combat system within the VR experience.
The Character Controller and Player Controller are linked with the CameraRig in Unity to manage player movement and perspective. Additionally, a custom script monitors the player’s health points. Should the player’s health deplete entirely, the game is programmed to automatically transition back to the start scene. This setup not only ensures a smooth gameplay experience but also integrates crucial game mechanics like health management and scene transitions seamlessly within the VR environment.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EggEnemy : MonoBehaviour
{
[SerializeField] int health = 3;
[SerializeField] int attackCD = 3;
[SerializeField] int attackrange = 2;
[SerializeField] int aggrorange = 4;
GameObject player;
Animator animator;
NavMeshAgent agent;
float timepass;
float newDisCD = 0.5f;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindWithTag("Player");
animator = GetComponent<Animator>();
agent = GetComponent<NavMeshAgent>();
}
void Update(){
animator.SetFloat("Movespeed", agent.velocity.magnitude/agent.speed);
if(timepass >= attackCD){
if (Vector3.Distance(player.transform.position, transform.position) <= attackrange){
animator.SetTrigger("attack");
timepass = 0;
}
}
timepass += Time.deltaTime;
if (newDisCD <= 0 && Vector3.Distance(player.transform.position, transform.position) <= aggrorange){
newDisCD = 0.5f;
agent.SetDestination(player.transform.position);
}
newDisCD -= Time.deltaTime;
}
public void TakeDamage(int damage){
health -=damage;
animator.SetTrigger("damage");
if (health <= 0){
animator.SetTrigger("Dealth");
StartCoroutine(DestroyAfterAnimation());
}
}
private IEnumerator DestroyAfterAnimation()
{
yield return new WaitForSeconds(2f);
Destroy(this.gameObject);
}
// Update is called once per frame
}
The Nav Mesh component in Unity is utilized to enable intelligent navigation for characters within the game environment. This is complemented by the custom ‘Egg Enemy’ script, which is specifically designed to enhance the AI behavior. This script dictates the actions and reactions of the enemies, making them more dynamic and challenging, thereby enriching the overall gaming experience with sophisticated and responsive AI-driven characters.
The Hand Pose API tools from Oculus are employed to customize hand poses. These hand poses are then connected to a shape recognizer, which is integral to the gameplay. The recognizer is linked to an active state selector, enabling dynamic interaction based on the player’s hand movements. This is further encapsulated using an active state wrapper, which is responsible for triggering specific functions within the game. This innovative use of the Hand Pose API tools enhances the immersive and interactive experience of the VR game.