- Home /
Fly-Through using Unity and Oculus Rift
Hey all,
I'm currently trying to create a fly-through demo with Unity and Oculus Rift, where the user can virtually fly though a certain CAD model inside a small room. Everything is based on the OculusUnityIntegration.unitypackage, including all the assets and prefabs, most importantly the OVRPlayerController and OVRCameraController. So far, I kicked out the "unnecessary" code and disabled gravity. Furthermore, I have implemented a Jump_Up()
and Jump_Down()
method, which are used to ascend and descent the camera.
My problem now is that I'd like to always move in the direction of the camera when pressing W
for forward, however I couldn't achieve that kind of movement yet. Basically it should be the same experience as in the Unity scene editor itself, where you can "fly" in direction of the camera when holding right mouse down and navigation via W,A,S,D
.
I'm fairly new to both the Oculus SDK and game development in general, so any tips are appreciated!
I've made lots of different changes in the code, and I assume some changes are necessary to the OVRPlaerController.cs
file, mainly to Update()
or UpdateMovement():
protected virtual void Update()
{
UpdateMovement();
Vector3 moveDirection = Vector3.zero;
float motorDamp = (1.0f + (Damping * OVRDevice.SimulationRate * Time.deltaTime));
MoveThrottle.x /= motorDamp;
MoveThrottle.y = (MoveThrottle.y > 0.0f) ? (MoveThrottle.y / motorDamp) : MoveThrottle.y;
MoveThrottle.z /= motorDamp;
moveDirection += MoveThrottle * OVRDevice.SimulationRate * Time.deltaTime;
moveDirection.y += FallSpeed * OVRDevice.SimulationRate * Time.deltaTime;
Vector3 predictedXZ = Vector3.Scale((Controller.transform.localPosition + moveDirection),
new Vector3(1, 0, 1));
// Move contoller
Controller.Move(moveDirection);
Vector3 actualXZ = Vector3.Scale(Controller.transform.localPosition, new Vector3(1, 0, 1));
if (predictedXZ != actualXZ)
MoveThrottle += (actualXZ - predictedXZ) / (OVRDevice.SimulationRate * Time.deltaTime);
// Update rotation using CameraController transform, possibly proving some rules for
// sliding the rotation for a more natural movement and body visual
UpdatePlayerForwardDirTransform();
}
public virtual void UpdateMovement()
{
// Do not apply input if we are showing a level selection display
if(HaltUpdateMovement == true)
return;
bool moveForward = false;
bool moveLeft = false;
bool moveRight = false;
bool moveBack = false;
MoveScale = 5.0f;
// * * * * * * * * * * *
// Keyboard input
// Move
// WASD
if (Input.GetKey(KeyCode.W)) moveForward = true;
if (Input.GetKey(KeyCode.A)) moveLeft = true;
if (Input.GetKey(KeyCode.S)) moveBack = true;
if (Input.GetKey(KeyCode.D)) moveRight = true;
// Arrow keys
if (Input.GetKey(KeyCode.UpArrow)) moveForward = true;
if (Input.GetKey(KeyCode.LeftArrow)) moveLeft = true;
if (Input.GetKey(KeyCode.DownArrow)) moveBack = true;
if (Input.GetKey(KeyCode.RightArrow)) moveRight = true;
MoveScale *= OVRDevice.SimulationRate * Time.deltaTime;
// Compute this for key movement
float moveInfluence = Acceleration * 0.1f * MoveScale * MoveScaleMultiplier;
if(DirXform != null)
{
if (moveForward)
MoveThrottle += DirXform.TransformDirection(Vector3.forward * moveInfluence * transform.lossyScale.z);
if (moveBack)
MoveThrottle += DirXform.TransformDirection(Vector3.back * moveInfluence * transform.lossyScale.z) * BackAndSideDampen;
if (moveLeft)
MoveThrottle += DirXform.TransformDirection(Vector3.left * moveInfluence * transform.lossyScale.x) * BackAndSideDampen;
if (moveRight)
MoveThrottle += DirXform.TransformDirection(Vector3.right * moveInfluence * transform.lossyScale.x) * BackAndSideDampen;
}
// Rotate
// compute for key rotation
float rotateInfluence = OVRDevice.SimulationRate * Time.deltaTime * RotationAmount * RotationScaleMultiplier;
float deltaRotation = 0.0f;
if(SkipMouseRotation == false)
deltaRotation = Input.GetAxis("Mouse X") * rotateInfluence * 3.25f;
float filteredDeltaRotation = (sDeltaRotationOld * 0.0f) + (deltaRotation * 1.0f);
YRotation += filteredDeltaRotation;
sDeltaRotationOld = filteredDeltaRotation;
// Update cameras direction and rotation
if (Input.GetKey(KeyCode.F))
{
Jump_Up();
Jump_Up();
}
if (Input.GetKey (KeyCode.H))
{
Jump_Down ();
Jump_Up();
}
SetCameras();
}
Thanks a lot in advance!!
Hey mate, did you have any luck with this? I'm also trying to achieve this!
Have a look at the standard assets
You could just modify the First Person Controller
Answer by Wisearn · Nov 26, 2014 at 11:00 AM
Ok so I don't know what else might be wrong with your code but you want to use the transform.up transform.right transform.forward insted of Vector3.up/right/forward
To get LEFT you do "minus transform.right"
-transform.right
And same with backwards&down
Vector3.forward will give a "forward" direction that is independant of any rotation, transform.forward on the other hand will give you the direction that is forward of the transform (in this case your camera.transform)
Answer by usagi · May 01, 2015 at 01:01 PM
I'm also looking about adding flying capabilities to my camera and tried the last hint about using transfrom instead of Vector3 but I had no luck.
Any other suggestions?
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Orthographic Camera 2 Answers
Disabling VR Camera rotation creates ''cinema'' effect in VR device 1 Answer