Last active
July 11, 2016 18:16
-
-
Save brean/5210d01240116573d61556173837745e to your computer and use it in GitHub Desktop.
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
// 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