Skip to content

Instantly share code, notes, and snippets.

@brean
Last active July 11, 2016 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brean/5210d01240116573d61556173837745e to your computer and use it in GitHub Desktop.
Save brean/5210d01240116573d61556173837745e to your computer and use it in GitHub Desktop.
// keep object in fixed distance to camera (including camera rotation - useful to place object for Hololens at the start of the application)
using UnityEngine;
using System.Collections;
public class FixedTagalong : MonoBehaviour {
Vector3 startpos;
public float smoothTime = 0.4f;
private Vector3 velocity = Vector3.one;
void Start() {
startpos = this.transform.position;
}
void Update() {
// move cube to camera
Quaternion toQuat = Camera.main.transform.rotation;
Vector3 targetPosition = Camera.main.transform.position;
// move according to start position
targetPosition += Camera.main.transform.forward * startpos.z;
targetPosition += Camera.main.transform.up * startpos.y;
targetPosition += Camera.main.transform.right * startpos.x;
// smoothly rotate and position in front of camera
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
transform.rotation = Quaternion.Slerp(transform.rotation, toQuat, smoothTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment