Virtual reality under Unity
12705 단어 Unity
Virtual reality under Unity
Example project: W.A.L.L.
Demo video
W.A.L.L. is a two-player game. Player 1 has the ability to build structures, while player 2 is a knight who can walk around the world and shot with his crossbow in virtual reality.
Equipment used
Oculus rift
The oculus rift is a popular virtual reality device with two essential features:
Razer hydra
The razer hydra is a gaming device which is made of an emitter and two controllers. It uses a magnetic field to track the position and rotation of the controllers. Each one of them also have several buttons and a joystick.
In W.A.L.L., the controllers are used to move around, jump, open doors, aim and shot with the crossbow.
Implementation
GitHub repository
Oculus rift
Using oculus rift in Unity is very straightforward. An integration package exists, so downloading it and replace the game camera by the Unity prefab it contains is all it takes to get started.
An extra effort was necessary to make the model's head follow the camera though, as their orientation were different.
HeadBehaviour.cs
public class HeadBehaviour : MonoBehaviour {
public Camera playerCamera;
void LateUpdate () {
transform.localRotation = playerCamera.transform.localRotation;
// Fixes the head orientation
transform.localRotation = new Quaternion(-transform.localRotation.y, transform.localRotation.z, -transform.localRotation.x, transform.localRotation.w);
}
}
Razer hydra
A Unity package exists for the hydra too, and comes with an API which is easy to use.
SixenseInput.cs
...
/// <summary>
/// Value of trigger from released (0.0) to pressed (1.0).
/// </summary>
public float Trigger { get { return m_Trigger; } }
/// <summary>
/// Value of joystick X axis from left (-1.0) to right (1.0).
/// </summary>
public float JoystickX { get { return m_JoystickX; } }
/// <summary>
/// Value of joystick Y axis from bottom (-1.0) to top (1.0).
/// </summary>
public float JoystickY { get { return m_JoystickY; } }
/// <summary>
/// The controller position in Unity coordinates.
/// </summary>
public Vector3 Position { get { return new Vector3( m_Position.x, m_Position.y, -m_Position.z ); } }
...
Those methods are called in the knight controller scripts.Moving and jumping:
FPSInputController.cs
SixenseInput.Controller controller = SixenseInput.GetController(SixenseHands.LEFT);
if(controller != null) {
// Get the input vector from Razer Hydra
directionVector = new Vector3(controller.JoystickX, 0, controller.JoystickY);
motor.inputJump = controller.GetButton(SixenseButtons.BUMPER);
}
...
// Apply the direction to the CharacterMotor
motor.inputMoveDirection = transform.rotation * directionVector;
Opening doors:Player.cs
if((controller != null && controller.GetButtonDown(SixenseButtons.TRIGGER))
|| Input.GetButton("Door")) {
RaycastHit hit = new RaycastHit();
if(Physics.Raycast(playerCamera.position, playerCamera.forward, out hit, 500)) {
if(hit.collider.gameObject.CompareTag("door") && hit.distance <= maxDoorDistance) {
GameObject test = hit.collider.gameObject;
hit.collider.gameObject.SendMessageUpwards("interact");
}
}
}
Shooting:CrossBow.cs
SixenseInput.Controller controller = SixenseInput.GetController(SixenseHands.RIGHT);
if(controller != null && controller.GetButtonDown(SixenseButtons.TRIGGER)
|| Input.GetKeyDown(KeyCode.Mouse0)) {
fire();
audio.Play();
}
The user also needs to do a calibration with each end at the beginning of the game.
Reference
이 문제에 관하여(Virtual reality under Unity), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Maerig/items/fc8b6d2cdb5510e0028e텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)