Forum Discussion
UberLax
12 years agoHonored Guest
Subclassing OVRPlayerController
For my game, I want to subclass OVRPlayerController to make my own player controller. I want to keep all the benefits that the OVRPlayerController provides while implementing my own custom functionality (like custom sprinting). Also, as long as the API doesn't change, I can update my Oculus Rift SDK without worrying about my code breaking. I do not get this benefit if I modify OVRPlayerController.cs myself.
Unfortunately OVRPlayerController uses private variables in many functions (like UpdateMovement). I am unable to maintain similar functionality in my subclass of OVRPlayerController because I cannot modify these variables. Ideally, any variables used in public functions would be made protected so that a subclass of OVRPlayerController can modify them.
Obviously I can change the script myself, and I probably will for the time being. My hope is that someone at Oculus will see this post and they can make a more extensible API for us.
If I am missing something here, please let me know. My goal is to solve this problem, not to complain.
If you are having the same issue or a similar one, please post and share!
Unfortunately OVRPlayerController uses private variables in many functions (like UpdateMovement). I am unable to maintain similar functionality in my subclass of OVRPlayerController because I cannot modify these variables. Ideally, any variables used in public functions would be made protected so that a subclass of OVRPlayerController can modify them.
Obviously I can change the script myself, and I probably will for the time being. My hope is that someone at Oculus will see this post and they can make a more extensible API for us.
If I am missing something here, please let me know. My goal is to solve this problem, not to complain.
If you are having the same issue or a similar one, please post and share!
7 Replies
Replies have been turned off for this discussion
- diablosv21Honored GuestHere's what I'm thinking you could do.
Make OVRPlayerController a partial class, that means you'll have to update it each time you download a new version but at least you will get a compiler error if you forget to do it and it's quick and easy to do.
But partial won't be enough because no doubt you want to override stuff as well. So use your own partial class to elevate the private variables you need from private to protected, then make another class to inherit from OVRPlayerController and you can go about your overriding and customised actions.
So in code...
OVRPlayerController.cspublic partial class OVRPlayerController { ... }
OVRPlayerControllerExtended.cspublic partial class OVRPlayerController {
// Elevate the access of the variables you need
protected float Whatever {
get { return _privateWhatever; }
set { _privateWhatever = value; }
}
}
MyOVRPlayerController.cspublic class MyPlayerController : OVRPlayerController {
// Do all your customisation here
public new void update {
var variable = Whatever; // private variable should be accessible
base.Update(); // If you want...
}
}
I can't test this at the moment to see if it works but it should be fine. - cyberealityGrand ChampionHonestly the OVRPlayerController is more of an example than anything that should make it into a final game. You can use it as a basis for your code, but you will likely need to do a lot of custom stuff to integrate it well into your game.
- kengrimmHonored GuestThanks for the info cyberreality. I'm new to Unity and the Rift so please bear with me and I'm new to the forum so I'm tacking on to this post as I'm also in a similar boat. I simply want my FPS character to swim. I could easily do this with a custom rigidbody / collider based character attached to the OVR camera controller but I worry about a lot of the custom Rift code in the player controller that could potentially degrade the experience. Modifying the OVR player controller has me really worried as it is a script with not much customizability and I do not want to have to deal with mods every time I upgrade. Is my best bet to define my own custom rigidbody / collider based FPS controller and then attach that to the OVR camera or should I stick with modifying the OVR character controller? Won't I be running into all sorts of Rift specific issues if I only rely on the camera controller?
Thanks in advance! - cyberealityGrand ChampionThe easiest thing to do is to use the Unity FirstPersonController and replace the MainCamera with the OVRCameraController. There are just a few small modifications you need to get this to work. See this post: viewtopic.php?f=37&t=3018#p37280
I have not tested this with swimming but it works with moving platforms and stuff like that so it may be all you need. - kengrimmHonored GuestThanks, I will go that direction. Is there any reason behind why you'd use the Unity FPController over the OVRPlayerController that comes in the Unity OVR integration package?
- cyberealityGrand ChampionI know specifically that the OVRPlayerController doesn't work well with moving platforms (ie elevators, etc.) while the Unity FPController does. I didn't look too deep to find out why. I bet you could fix the OVRPlayerController with a little work but haven't explored it myself.
- kengrimmHonored GuestThanks. The solution you gave above worked for me.
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
- 4 years ago
- 7 years agoAnonymous