Forum Discussion
zalo
13 years agoExplorer
Gravity Compensated Acceleration Vector
Here's the code to get a gravity compensated acceleration vector from the Oculus Sensor:
Drag the RightCamera object in the Rift Camera Prefab to the "RiftCam" slot. It needs this to use the orientation of the Rift to compensate for gravity. Not the best way, but InversePointTransform only takes transforms, so the RightCam already has one pre-made.
Enjoy.
using UnityEngine;
using System.Collections;
public class Derp : MonoBehaviour {
public GameObject RiftCam;
float x = 0 ;
float y = 0;
float z = 0;
// Update is called once per frame
void Update () {
OVRDevice.GetAcceleration(0, ref x, ref y, ref z);
Vector3 Down = new Vector3(RiftCam.transform.position.x, RiftCam.transform.position.y+9.81f, RiftCam.transform.position.z);
Down = RiftCam.transform.InverseTransformPoint(Down);
Vector3 GravityCompensatedAcceleration = new Vector3(x-Down.x,y-Down.y,z+Down.z);
}
}
Drag the RightCamera object in the Rift Camera Prefab to the "RiftCam" slot. It needs this to use the orientation of the Rift to compensate for gravity. Not the best way, but InversePointTransform only takes transforms, so the RightCam already has one pre-made.
Enjoy.
2 Replies
Replies have been turned off for this discussion
- rincewindHonored Guestnoob-question: how to run the code? :)
I've added the script into the OVRcc right below the OVRDevice script and then dragged the Right Camera into RiftCam slot, but it does nothing. - zaloExplorerOh it doesn't do anything by itself.
That snippet just gives you the vector by itself.
Just copy the salient bits between "public class" and the last "}" into your own scripts and interactions.
Or, if you want, put the whole thing into Derp.cs and add Debug.Log(GravityCompensatedAcceleration); to the end before the last "}".
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 17 days ago
- 3 years ago