Skip to content

Instantly share code, notes, and snippets.

@lordlycastle
Created June 12, 2020 19:34
Show Gist options
  • Save lordlycastle/758a5f7f560d06b7a794c67dfed702e1 to your computer and use it in GitHub Desktop.
Save lordlycastle/758a5f7f560d06b7a794c67dfed702e1 to your computer and use it in GitHub Desktop.
Add moving stuff as child with Z offset which will be distance from headset. Use this to rotate the root gameobject.
using DG.Tweening;
using Sirenix.OdinInspector;
using UnityEngine;
public class MoveItemToView : MonoBehaviour
{
[Header("Found dynamically.")]
public Transform headsetTransform;
public GameObject itemParent;
private void Start ()
{
DOTween.Sequence()
.SetDelay(0.1f)
.AppendCallback(() =>
{
if (itemParent)
{
itemParent = gameObject;
}
headsetTransform = VRTK.VRTK_DeviceFinder.HeadsetTransform();
itemParent.transform.position = headsetTransform.position;
})
.Play();
}
void Update ()
{
if (headsetTransform == null)
return;
var newAngle = Quaternion.LerpUnclamped(itemParent.transform.rotation,
headsetTransform.rotation, Time.deltaTime);
itemParent.transform.rotation = newAngle;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment