cancel
Showing results for 
Search instead for 
Did you mean: 

Comfortable forced crouching?

DillonRobinson
Honored Guest
Hello guys,

I have a question here. As I near the end of my next project, I have an idea. In a couple areas throughout my diorama game, I have placed some chairs. I disabled their collision boxes, but that's not quite enough.

What I wish to do is create "zones" where crouching is forced, and place these invisible zones/collision objects at the base of these chairs, so that when the chairs are walked into, the player's height is, preferably smoothly, lowered to a desired value, based on the height of that chair.

The desired effect being to allow the player to "sit" in the chair by simply walking into it, and standing back up by just walking out of the object.



This isn't necessarily a topic to say "WRITE IT FOR ME!" (though I wouldn't turn that down 😉 ), but I'm asking for some guidance here in regards to which exact functions I should use, and if there's any "gotcha"s in regards to doing this with the OVR player controller when, as far as I know, player height is also grabbed from the Oculus player profile.

Any help would be appreciated, as I believe this 'small' feature would have great effect in my current project.
7 REPLIES 7

cybereality
Grand Champion
Yeah, that sounds possible.

You'll just have to edit the OVRPlayerController to support ducking.

Though, you should always be careful about any time you take over control of the camera (i.e scripted animations) as they can cause some people discomfort.

DillonRobinson
Honored Guest
Though, you should always be careful about any time you take over control of the camera (i.e scripted animations) as they can cause some people discomfort.


This is why I'm aiming for an ease-in and ease-out motion. I'm not quite sure yet if that would feel better, or to have the view snap to the lowered height automatically. But I guess that's something I'll have to test! It's difficult to pinpoint moments that cause discomfort when I have never gotten any sort of motion sickness from the Rift.

EDIT

Wow! So I was using if (forceCrouch = true) {} else {}
All I did was remove the "=true" and it started working! Very bizarre. I could have sworn they both meant the same thing.

I'm no longer in need of help, I trial-and-error'd my through it, it took 3 hours since I do not know C#, but it's working now 🙂


In case anyone else needs to do something similar:

Open OVRPlayerController.cs

Find where the var declarations are happening
(public class OVRPlayerController : OVRComponent
{...
)

There, I tacked on
////////////////////////////////////////////
public float heightStand;
public float heightCrouch;
public bool forceCrouch = false;
//////////////////////////////////////////


And at "new public virtual void Start()", added the lines:
forceCrouch = false;
heightStand = Controller.height;
heightCrouch = 0.5f;



I then made my cube and checked off "Is Trigger" in the Box Collider settings. I created a new tag CrouchZone, and added it to my collision box.

I added some lines between that mentioned Start() and the upcoming Update() with:

  void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "CrouchZone") {
forceCrouch = true;
print ("zone hit!");
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "CrouchZone") {
forceCrouch = false;
print ("zone left!");
}
}


Under the existing "new public virtual void Update()", I added:
if (forceCrouch) {
Controller.height = heightCrouch;
print ("forceCrouch:" + forceCrouch);

} else {
Controller.height = heightStand;
print ("forceCrouch:" + forceCrouch);

}


So what this does is, at runtime, sets a value based on the players height, which I can only guess is the same value grabbed from the Oculus profiler. When you hit the crouch zone, your height is halved to simulate taking a seat, and when you leave the zone, it's returned to what it was when the game began.

I wanted to do this so that you can both stand up and walk around, or take a seat and soak it in, like my last demo. It works pretty well for my purposes, but it's a bit buggy floor-collision-wise if you do it rapidly and try to run around while toggling it.

Note
I upped the Player Controller's "Skin Width" and doubled it from the default, as my player was falling through the floor on random crouches.

grodenglaive
Honored Guest
VRCinema does this beautifully. It doesn't just change your height it sits you in the seat facing forward (smoothly). Perhaps use a lerp function when you hit the chair collider.

drash
Heroic Explorer
Thanks for sharing your code for this!
"DillonRobinson" wrote:
Wow! So I was using if (forceCrouch = true) {} else {}
All I did was remove the "=true" and it started working! Very bizarre. I could have sworn they both meant the same thing.

When you do if(forceCrouch = true), it's actually setting forceCrouch to true, and then checking if it's true. This is because of the single equal sign (assignment operator). To simply check equivalence, use a double equal sign instead. The reason it works without the "= true" is because everything inside the if's parentheses are evaluated to a true/false condition.

DillonRobinson
Honored Guest
"drash" wrote:
To simply check equivalence, use a double equal sign instead. The reason it works without the "= true" is because everything inside the if's parentheses are evaluated to a true/false condition.


Ahhh thank you for the explanation! It's starting to come back from college 6 years ago 😉

I also assumed that anything within the's if's "(...)" was a condition or operator, and that variables (like a boolean) couldn't be changed in there. Good to know!

I'm sure I'm using C# in the least efficient way possible, but I did indeed get this working, and it seemed to add the slightest of transition between standing up and sitting down. No more than 2 frames transition, but I don't think I need to make it too smooth and this seems comfortable if you know you're walking into a crouching zone.

I also just wrote a script to attach to light switch meshes, link a few components (the light object they relate to, and that light's "bulb" mesh), and now I have working light switches!

At the rate I'm finishing this at, I considered removing the crouch script since DK2 is on the horizon, but figured people don't want to physically sit down from a standing position just to sit, so I'm leaving it in and seeing how it feels with positional tracking.

Boff
Honored Guest
Thanks for posting your code, I'm sure I'll be needing this sometime in the future!

DillonRobinson
Honored Guest
No problem man! Looking at it now, it's actually quite simple, but I was not familiar with the PlayerController setup at all, and know nothing about C#, so I'm just glad it works.

This doesn't even lock you to a point, like a few I've seen do. There's nothing wrong with that, but I prefer to be able to nudge the player around within this "seating zone" so that they can get a better look at things.

I'm just worried now about DK2. I'll have to see how we need to change these objects around for DK2 implementation, and if any OVRPlayerController code needs to be changed.