/// <summary> /// Place an object on the HoloSurface. /// </summary> public static bool PlaceOnSurface(UnityEngine.Transform target, bool applyXRotation = false, bool applyYRotation = false, bool applyZRotation = false) { UnityEngine.RaycastHit hit; if (HoloUtils.FindPointOnSurface(50.0f, out hit)) { target.position = hit.point; UnityEngine.Quaternion toQuat = UnityEngine.Quaternion.FromToRotation(UnityEngine.Vector3.back, hit.normal); if (!applyXRotation) { toQuat.x = 0; } if (!applyYRotation) { toQuat.y = 0; } if (!applyZRotation) { toQuat.z = 0; } if (applyXRotation || applyYRotation || applyZRotation) { target.rotation = UnityEngine.Quaternion.Lerp(target.rotation, toQuat, 0.5f); } return(true); } return(false); }
/// <summary> /// Places this object on a surface. /// </summary> public override void Update() { //when user selects this object. if (_isSelected) { //Check if we can place this object on a surface... if (HoloUtils.PlaceOnSurface(this.transform) == false) { //if no surface to place, place this in front of user. HoloUtils.PlaceInFrontOfUser(this.transform, 5.0f); } } }
/// <summary> /// Places an object in from of the user at X distance /// </summary> /// <param name="subject">UnityEngine.Transform</param> /// <param name="distance">float</param> public static void PlaceInFrontOfUser(UnityEngine.Transform subject, float distance, bool applyXRotation = false, bool applyYRotation = false, bool applyZRotation = false) { //place window in front of user. UnityEngine.Transform user_transform = UnityEngine.Camera.main.transform; UnityEngine.Vector3 user_position = user_transform.position; UnityEngine.Quaternion user_rotation = user_transform.rotation; UnityEngine.Vector3 toPos = user_position + (user_transform.forward * distance); //set location and rotation. subject.position = toPos; if (!applyXRotation) { user_rotation.x = 0; } if (!applyYRotation) { user_rotation.y = 0; } if (!applyZRotation) { user_rotation.z = 0; } if (applyXRotation || applyYRotation || applyZRotation) { subject.rotation = user_rotation; } //check if a surface is present closer than distance, // if so... we place the subject on that surface. UnityEngine.RaycastHit hit; if (HoloUtils.FindPointOnSurface(distance, out hit)) { UnityEngine.Vector3 deltaPosition = user_transform.position - hit.point; if (hit.distance < distance) { subject.position = hit.point; } } }