- Home /
Tracking Forward HTC Vive Movement
Hi! I'm trying to use an HTC Vive to track a player's forward movement inside a game. Right now the code I have works in a static x-direction, where your positive x movement would play a "walking forward" animation, negative x movement would play a "walking backward" animation, and a general "idle" trigger for when the hmd doesn't detect significant movement. Is there a way to track the "forward" direction the player will walk in, as opposed to hard coding a single x-direction?
Here's what I have so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimatedController : MonoBehaviour
{
//how often we want the function to be called
[SerializeField] float refresh_rate = 0.5f;
//where we place the "camera" object under Camera Rig for SteamVR
[SerializeField] Transform HMD;
//[SerializeField] Transform build;
[SerializeField] float error_margin = 0.3f;
[SerializeField] float out_of_bounds = 1.0f;
float xDir, yDir, zDir, xTemp, yTemp, zTemp;
// Start is called before the first frame update
void Start()
{
//after 0.1f seconds, calls the function TrackMovement every refresh_rate value
InvokeRepeating("TrackMovement", .01f, refresh_rate);
//retrieve the values of x, y, and z for the HMD
xDir = HMD.transform.position.x;
yDir = HMD.transform.position.y;
zDir = HMD.transform.position.z;
}
void TrackMovement()
{
//retrieve values for comparison
xTemp = HMD.transform.position.x;
yTemp = HMD.transform.position.y;
zTemp = HMD.transform.position.z;
//If the refreshed value is greater than the current + idle margin error - forward
if(xTemp > xDir + error_margin)
{
Debug.Log("Trigger Forward Walking Animation");
}
//If the refreshed value is less than the current - idle margin error - backward
if (xTemp < xDir - error_margin)
{
Debug.Log("Trigger Backward Walking Animation");
}
//if the refreshed value is inbetween idle margin errors - idle
if (xTemp <= xDir + error_margin && xTemp >= xDir - error_margin)
{
Debug.Log("Trigger Idle Animation");
}
//if the refreshed value is out of bounds - falling
if (zTemp > zDir + out_of_bounds || zTemp < zDir - out_of_bounds)
{
Debug.Log("Trigger Falling Animation");
}
xDir = xTemp;
yDir = yTemp;
zDir = zTemp;
}
}
Comment
Your answer
Follow this Question
Related Questions
Can I make animations snap to a frame? 1 Answer
NPC walk animation 3 Answers
2D eyes on 3D character? 2 Answers
Animating different Weapons 2 Answers
Enemy should follow the Player if he is in range, but how? 2 Answers