Skip to content

Instantly share code, notes, and snippets.

@WhiteNoise
Created May 23, 2017 12:44
Show Gist options
  • Select an option

  • Save WhiteNoise/43a1b028db416cf0e4aee69fc8cce336 to your computer and use it in GitHub Desktop.

Select an option

Save WhiteNoise/43a1b028db416cf0e4aee69fc8cce336 to your computer and use it in GitHub Desktop.
Simple Trackers IK
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TrackerPuppet : MonoBehaviour {
[Header("Trackers")]
public GameObject controllerLeft;
public GameObject controllerRight;
public GameObject trackerLeft;
public GameObject trackerRight;
public GameObject playerHead;
private float myHeight = 0.81f;
[Header("Bones")]
public Transform main;
public Transform root;
public Transform leftUpperArm;
public Transform leftForeArm;
public Transform rightUpperArm;
public Transform rightForeArm;
public Transform head;
public Transform neck;
public Transform chest;
public Transform hips;
private Quaternion startingChestRotation;
private Quaternion startingHipsRotation;
// Use this for initialization
void Start () {
trackerLeft.GetComponentInParent<SteamVR_TrackedObject>().SetDeviceIndex(
SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.FarthestLeft, Valve.VR.ETrackedDeviceClass.GenericTracker) );
trackerRight.GetComponentInParent<SteamVR_TrackedObject>().SetDeviceIndex(
SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.FarthestRight, Valve.VR.ETrackedDeviceClass.GenericTracker) );
controllerLeft.GetComponentInParent<SteamVR_TrackedObject>().SetDeviceIndex(
SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Leftmost, Valve.VR.ETrackedDeviceClass.Controller) );
controllerRight.GetComponentInParent<SteamVR_TrackedObject>().SetDeviceIndex(
SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost, Valve.VR.ETrackedDeviceClass.Controller) );
startingChestRotation = chest.localRotation;
startingHipsRotation = hips.rotation;
}
// Update is called once per frame
void Update () {
leftUpperArm.rotation = trackerLeft.transform.rotation ;
leftForeArm.rotation = controllerLeft.transform.rotation;
rightUpperArm.rotation = trackerRight.transform.rotation ;
rightForeArm.rotation = controllerRight.transform.rotation;
head.rotation = playerHead.transform.rotation;
Vector3 shoulderOffset = trackerRight.transform.parent.localPosition - trackerLeft.transform.parent.localPosition;
Vector3 shoulderLine2d = shoulderOffset;
Vector3 facing = Vector3.Cross( shoulderLine2d.normalized, Vector3.up);
Quaternion hipsRot = Quaternion.FromToRotation(Vector3.forward, facing);
shoulderLine2d.y = 0.0f;
Vector3 shoulderCenter = (trackerRight.transform.parent.localPosition + trackerLeft.transform.parent.localPosition)/2.0f;
//Vector3 shoulderCenter = (trackerRight.transform.position + trackerLeft.transform.position)/2.0f;
float leftRightTiltAng = Vector3.Angle(shoulderOffset, shoulderLine2d);
if(shoulderOffset.y > 0.0f)
leftRightTiltAng *= -1.0f;
Vector3 headPos = playerHead.transform.parent.localPosition;
Vector3 headOffset = headPos - shoulderCenter;
float leanRotation = 90.0f * Vector3.Dot(facing, headOffset.normalized);
chest.localRotation = startingChestRotation * Quaternion.Euler(0.0f, -leanRotation, leftRightTiltAng);
float facingAng = Vector3.Angle(Vector3.forward, facing);
if(Vector3.Dot(facing, Vector3.right) < 0.0f) {
facingAng = -facingAng;
}
hips.rotation = startingHipsRotation * Quaternion.Euler(facingAng, 0.0f, 0.0f);
main.localPosition = new Vector3(headPos.x, Mathf.Max( headPos.y - myHeight, 0.0f), headPos.z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment