Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
Ohyeahitsjosh's avatar
Ohyeahitsjosh
Honored Guest
10 years ago

Teleport by leaning. Need help with C#

Hello everyone! Happy Pre-order day.

I need some help with some code that I'm writing. Previously, I had posted here about asking for help on some code that would scale the player depending on whether or not users were standing or sitting. See this link for what I'm talking about. https://forums.oculus.com/viewtopic.php?f=37&t=25251#p287593. I thought I could alter this script to detect whether or not users are leaning and then teleport them through a portal in my unity project.

I altered the code slightly so it would work in Z instead of Y and I found that I kept getting different and inconsistent values each time I started up the scene. So I had to change the code slightly again and here's where I'm at currently:

using UnityEngine;
using System.Collections;
using UnityEngine.VR;

public class LeaningorNotLeaning : MonoBehaviour {

public float Leaning;

private float seatedZ = float.NegativeInfinity;

void Start()
{
RecenterLeaning();
}

void Update()
{
if (seatedZ == float.NegativeInfinity) {
return;
}
float z = InputTracking.GetLocalPosition(0).z;
// x = transform.localPosition.x;
float zMaxSeated = seatedZ + 0.05f;
float zMinLeaning = seatedZ + 0.4f;
float rawLeaning = (z - zMaxSeated) / (zMinLeaning - zMaxSeated);
Leaning = Mathf.Clamp(rawLeaning, 0f, 1f);
Debug.Log ("LeaningOrNot: " + z + " maxSeat,minLean " + zMaxSeated + "," + zMinLeaning + " : leaning=" + Leaning + "; raw=" + rawLeaning);
}

public void RecenterLeaning()
{
StartCoroutine(RecenterLeaningCoroutine());

}

IEnumerator RecenterLeaningCoroutine()
{
while (! Input.GetKey(KeyCode.C)) {
yield return new WaitForSeconds (0.1f);
}
Debug.Log("LeaningOrNot: recentering");
InputTracking.Recenter();
yield return new WaitForEndOfFrame();
seatedZ = InputTracking.GetLocalPosition(0).z;
// seatedY = transform.localPosition.x;
}

}


I don't want to press the "C" button every time I want the player to lean so Unity can detect it. Is there any way to change this code so that I get a consistent number without a button press? Thanks!
Replies have been turned off for this discussion