Created
April 26, 2017 01:47
-
-
Save mnstrspeed/c54ecbb9d25b263bb15465101a304b10 to your computer and use it in GitHub Desktop.
Implementation of simple gain locomotion (originally described in this talk at Vision Summit 2016: https://www.youtube.com/watch?v=At_Zac4Xezw)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class SimpleGainLocomotion : MonoBehaviour | |
{ | |
public Transform Head; | |
public float Gain = 1f; | |
void Update() | |
{ | |
Vector3 localPlayerPosition = this.transform.InverseTransformPoint(this.Head.position); | |
Vector3 floorPosition = new Vector3(localPlayerPosition.x, 0f, localPlayerPosition.z); | |
this.transform.localPosition = this.Gain * floorPosition; | |
} | |
#region Debug visualization | |
void OnDrawGizmos() | |
{ | |
if (!this.isActiveAndEnabled) | |
return; | |
Vector3 localPlayerPosition = this.transform.InverseTransformPoint(this.Head.position); | |
Gizmos.color = Color.white; | |
Gizmos.DrawLine(this.Head.position, localPlayerPosition); | |
var playArea = this.GetComponent<SteamVR_PlayArea>(); | |
if (playArea != null) | |
{ | |
Gizmos.color = Color.green; | |
DrawWireframe(playArea.vertices, this.Gain + 1f); | |
} | |
} | |
public void DrawWireframe(Vector3[] vertices, float scale) | |
{ | |
if (vertices == null || vertices.Length == 0) | |
return; | |
var offset = transform.TransformVector(Vector3.up * 2f); | |
for (int i = 0; i < 4; i++) | |
{ | |
int next = (i + 1) % 4; | |
var a = vertices[i] * scale; | |
var b = a + offset; | |
var c = vertices[next] * scale; | |
var d = c + offset; | |
Gizmos.DrawLine(a, b); | |
Gizmos.DrawLine(a, c); | |
Gizmos.DrawLine(b, d); | |
} | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment