Forum Discussion
Ohyeahitsjosh
10 years agoHonored Guest
Detecting whether users are standing up or sitting down
Hello!
I'm not new to game development but I AM pretty new to developing with the Oculus Rift and C# in general. I'm trying to create a script that detects movement when players are using the Oculus Rift. Basically, I want Unity to detect if players are standing, sitting, or moving in between. Accordingly, I want the player size to shrink or grow based on their physical position. So if the player is standing, they're ginormous and if they're sitting, they'll be small.
So far, I haven't had much luck. I'm using Unity 5.1 (but can go to previous versions if needed) with the UnityEngine.VR integration. Form what I've seen, the integration doesn't seem to detect the Oculus Rift moving in the positive or negative Y direction. Frankly, I'm not sure where to go from here. This is for a class project and I can't seem to figure this out. Can anyone point me in the right direction or help me write some code to do this?
Thanks!
I'm not new to game development but I AM pretty new to developing with the Oculus Rift and C# in general. I'm trying to create a script that detects movement when players are using the Oculus Rift. Basically, I want Unity to detect if players are standing, sitting, or moving in between. Accordingly, I want the player size to shrink or grow based on their physical position. So if the player is standing, they're ginormous and if they're sitting, they'll be small.
So far, I haven't had much luck. I'm using Unity 5.1 (but can go to previous versions if needed) with the UnityEngine.VR integration. Form what I've seen, the integration doesn't seem to detect the Oculus Rift moving in the positive or negative Y direction. Frankly, I'm not sure where to go from here. This is for a class project and I can't seem to figure this out. Can anyone point me in the right direction or help me write some code to do this?
Thanks!
16 Replies
Replies have been turned off for this discussion
- wheatgrinderExplorerobviously Unity knows where the HMD is in y space.. when I stand up my view rises..
I think its just a matter of finding which transform to watch...
You probably need the Oculus Utilities for Unity to get at the data you want, I dont know that the built in Unity VR support provides that.. though it probably does..
I have a scale player, seems the best approach to scaling the player is to place the OVRPlayerContoller as a child of an empty game object and then scale the empty game object. This works perfect for me (with an exception of a bug in the game pad controller script that is easily fixed)
FYI: I think this is a really neat idea I may try it for fun! - petereptProtegeYes, why not get the user to Recenter when they are seated at the start. Then you know the Y position in tracking space VR.InputTracking.GetLocalPosition. Now the Y increases by .5 they are standing.
- OhyeahitsjoshHonored Guest
"peterept" wrote:
Yes, why not get the user to Recenter when they are seated at the start. Then you know the Y position in tracking space VR.InputTracking.GetLocalPosition. Now the Y increases by .5 they are standing.
That sounds good but I'm not sure how to code that (I'm a total newbie). Additionally, I would want the script to handle it automatically without the user having to press a button. Is that possible? - OhyeahitsjoshHonored Guest
"wheatgrinder" wrote:
obviously Unity knows where the HMD is in y space.. when I stand up my view rises..
I think its just a matter of finding which transform to watch...
You probably need the Oculus Utilities for Unity to get at the data you want, I dont know that the built in Unity VR support provides that.. though it probably does..
I have a scale player, seems the best approach to scaling the player is to place the OVRPlayerContoller as a child of an empty game object and then scale the empty game object. This works perfect for me (with an exception of a bug in the game pad controller script that is easily fixed)
FYI: I think this is a really neat idea I may try it for fun!
A friend of mine and me both tried this approach. We watched where the HMD was moving in space and it seems everything BUT the y position transformed. - petereptProtege
"Ohyeahitsjosh" wrote:
That sounds good but I'm not sure how to code that (I'm a total newbie). Additionally, I would want the script to handle it automatically without the user having to press a button. Is that possible?
You can do it automatically (grab the position on startup), but you will find it is much better to recenter any way because the user is not always facing forwards. Most games do it automagically, by having a button like "Face Forward and Press Start".
Here 's a quick attempt to do it. This is completely untested as I don't have access to my Dk2...
Attach this to any gameobject. Then you can check the Standing float to get the users current situation. 0 = seated. 1 = standing. any where in between is the percentage of transition.
It will auto calibrate on on Start(), but you should call RecenterSeated() when the user is actually ready to start.using UnityEngine;
using UnityEngine.VR;
using System.Collections;
public class SeatedOrStanding : MonoBehaviour
{
[Tooltip("Normalized Seated, Standing or Transitioning. 0..1 = Seated to Standing")]
public float Standing;
private float seatedY;
void Start()
{
RecenterSeated();
}
void Update()
{
float y = UnityEngine.VR.InputTracking.GetLocalPosition(0).y;
// y = transform.localPosition.y;
float yMaxSeated = seatedY + 0.2f;
float yMinStanding = seatedY + 0.4f;
float rawStanding = (y - yMaxSeated) / (yMinStanding - yMaxSeated);
Standing = Mathf.Clamp(rawStanding, 0f, 1f);
}
public void RecenterSeated()
{
StartCoroutine(RecenterSeatedCoroutine());
}
IEnumerator RecenterSeatedCoroutine()
{
InputTracking.Recenter();
yield return new WaitForEndOfFrame();
seatedY = InputTracking.GetLocalPosition(0).y;
// seatedY = transform.localPosition.y;
}
} - OhyeahitsjoshHonored Guest
"peterept" wrote:
You can do it automatically (grab the position on startup), but you will find it is much better to recenter any way because the user is not always facing forwards. Most games do it automagically, by having a button like "Face Forward and Press Start".
Here 's a quick attempt to do it. This is completely untested as I don't have access to my Dk2...
Attach this to any gameobject. Then you can check the Standing float to get the users current situation. 0 = seated. 1 = standing. any where in between is the percentage of transition.
It will auto calibrate on on Start(), but you should call RecenterSeated() when the user is actually ready to start.using UnityEngine;
using UnityEngine.VR;
using System.Collections;
public class SeatedOrStanding : MonoBehaviour
{
[Tooltip("Normalized Seated, Standing or Transitioning. 0..1 = Seated to Standing")]
public float Standing;
private float seatedY;
void Start()
{
RecenterSeated();
}
void Update()
{
float y = UnityEngine.VR.InputTracking.GetLocalPosition(0).y;
// y = transform.localPosition.y;
float yMaxSeated = seatedY + 0.2f;
float yMinStanding = seatedY + 0.4f;
float rawStanding = (y - yMaxSeated) / (yMinStanding - yMaxSeated);
Standing = Mathf.Clamp(rawStanding, 0f, 1f);
}
public void RecenterSeated()
{
StartCoroutine(RecenterSeatedCoroutine());
}
IEnumerator RecenterSeatedCoroutine()
{
InputTracking.Recenter();
yield return new WaitForEndOfFrame();
seatedY = InputTracking.GetLocalPosition(0).y;
// seatedY = transform.localPosition.y;
}
}
I seems to track the Y position of the player but it doesn't grow or shrink them. I think I can add that in though. Hot Damn, peterept, I OWE YOU BIG TIME. Thank you so much for your help. I'll add in the size change effect and report back. - OhyeahitsjoshHonored GuestActually, what would be the best way to include or merge my previous size changing code into this new one? (Did I mention I'm a total idiot when it comes to programming? :? )
using UnityEngine;
using System.Collections;
public class ChangeSize : MonoBehaviour
{
public float maxScale = 10.0f;
public float minScale = 2.0f;
public float shrinkSpeed = 1.0f;
public GameObject player;
public GameObject eye;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
float angle = Vector3.Dot(Vector3.up, eye.transform.forward);
var playerScale = player.transform.localScale;
if(angle > .9){
player.transform.localScale += new Vector3(shrinkSpeed, shrinkSpeed, shrinkSpeed);
Debug.Log ("looking up");
}
else if(angle < -.9){
player.transform.localScale -= new Vector3(shrinkSpeed, shrinkSpeed, shrinkSpeed);
Debug.Log ("looking down");
}
}
} - petereptProtegeI see what your doing there.
Drag this script onto the same game object that has the SeatedOrStanding script. Then drag your player gameobject on to Player.using UnityEngine;
using System.Collections;
[RequireComponent(typeof(SeatedOrStanding))]
public class SeatedOrStandingPlayerScaler : MonoBehaviour
{
public float maxScale = 10.0f;
public float minScale = 2.0f;
public Transform Player;
private SeatedOrStanding seatedOrStanding;
void Awake()
{
seatedOrStanding = GetComponent<SeatedOrStanding>();
}
void Update ()
{
Player.localScale = Vector3.Lerp (Player.localScale, Vector3.one * (minScale + ((maxScale - minScale) * seatedOrStanding.Standing)), 0.1f);
}
}
That should do what you want I think. - OhyeahitsjoshHonored Guest
"peterept" wrote:
I see what your doing there.
Drag this script onto the same game object that has the SeatedOrStanding script. Then drag your player gameobject on to Player.using UnityEngine;
using System.Collections;
[RequireComponent(typeof(SeatedOrStanding))]
public class SeatedOrStandingPlayerScaler : MonoBehaviour
{
public float maxScale = 10.0f;
public float minScale = 2.0f;
public Transform Player;
private SeatedOrStanding seatedOrStanding;
void Awake()
{
seatedOrStanding = GetComponent<SeatedOrStanding>();
}
void Update ()
{
Player.localScale = Vector3.Lerp (Player.localScale, Vector3.one * (minScale + ((maxScale - minScale) * seatedOrStanding.Standing)), 0.1f);
}
}
That should do what you want I think.
Yep, that did the trick! I tip my hat to you sir and sincerely thank you for your help. - petereptProtegeNo worries!
It is an interesting idea scaling the player like that. I'll have to run it tonight on my DK2 to have a look.
All the best
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
- 2 years ago
- 1 year agoAnonymous
- 9 years ago