Skip to content

Instantly share code, notes, and snippets.

@LiekeVanmulken
Last active June 20, 2018 22:14
Show Gist options
  • Save LiekeVanmulken/fe1ec0b539c76dfe7b4218953826a1b6 to your computer and use it in GitHub Desktop.
Save LiekeVanmulken/fe1ec0b539c76dfe7b4218953826a1b6 to your computer and use it in GitHub Desktop.
Possible Aryzon improvements
using UnityEngine;
using Vuforia;
/// <summary>
/// Script that improves tracking on some devices.
/// On some devices the autofocus doesn't work properly with Vuforia making it not track, this fixes that.
///
/// To use it just add to any gameobject.
/// </summary>
public class VuforiaAutoFocusBugScript : MonoBehaviour
{
void Start()
{
var vuforia = VuforiaARController.Instance;
vuforia.RegisterVuforiaStartedCallback(OnVuforiaStarted);
vuforia.RegisterOnPauseCallback(OnPaused);
}
private void OnVuforiaStarted()
{
CameraDevice.Instance.SetFocusMode(
CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
}
private void OnPaused(bool paused)
{
if (!paused) // resumed
{
// Set autofocus mode again when app is resumed
CameraDevice.Instance.SetFocusMode(
CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
}
}
}
using System.Collections;
using UnityEngine;
/// <summary>
/// Script to move the camera around with the keyboard and mouse.
///
/// To use this add it to the Aryzon/ARCamera/Cameras gameobject.
/// And then set the following setting (for debugging) on the Aryzon gameobject->ARMode script, "Show Scan for marker" to false.
///
/// MouseMovements are copied from https://gist.github.com/M-Pixel/e8aa79890297b975994e .
/// </summary>
public class MovementScript : MonoBehaviour
{
public enum RotationAxes
{
MouseXAndY = 0,
MouseX = 1,
MouseY = 2
};
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationX = 0F;
float rotationY = 0F;
Quaternion originalRotation;
void Update()
{
#if !UNITY_EDITOR
return;
#endif
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
transform.position += transform.forward * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
transform.position += -transform.forward * Time.deltaTime;
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
transform.position += transform.right * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
transform.position += -transform.right * Time.deltaTime;
}
if (axes == RotationAxes.MouseXAndY)
{
// Read the mouse input axis
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX = ClampAngle(rotationX, minimumX, maximumX);
rotationY = ClampAngle(rotationY, minimumY, maximumY);
Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, -Vector3.right);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else if (axes == RotationAxes.MouseX)
{
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationX = ClampAngle(rotationX, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = ClampAngle(rotationY, minimumY, maximumY);
Quaternion yQuaternion = Quaternion.AngleAxis(-rotationY, Vector3.right);
transform.localRotation = originalRotation * yQuaternion;
}
}
void Start()
{
// Make the rigid body not change rotation
originalRotation = transform.localRotation;
// Set the camera to a decent position
#if UNITY_EDITOR
StartCoroutine(SetCamera());
#endif
}
private IEnumerator SetCamera()
{
// Wait so it doesn't get turned off by Vuforia
yield return new WaitForSeconds(1);
transform.position = new Vector3(0, 2, -1.5f);
transform.rotation.Set(30, 0, 0, 0);
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
using System.Collections;
using UnityEngine;
/// <summary>
/// Script to turn gameobjects back on in the Unity Editor because Vuforia turns them off.
/// Just add this to the ImageTarget and it will automatically turn on your gameobjects when in the editor.
/// </summary>
public class TurnOnScript : MonoBehaviour
{
void Start()
{
#if UNITY_EDITOR
StartCoroutine(TurnOn());
#endif
}
IEnumerator TurnOn()
{
// Wait so it doesn't get turned off by Vuforia
yield return new WaitForSeconds(1);
TurnOnRecursively(transform);
}
/// <summary>
/// Turn on the collider and renderer for all descendants
/// </summary>
/// <param name="current"></param>
private void TurnOnRecursively(Transform current)
{
// Turn on the Renderer and Collider
foreach (Transform child in current)
{
var renderer = child.GetComponent<Renderer>();
if (renderer != null)
{
renderer.enabled = true;
}
var collider = child.GetComponent<Collider>();
if (collider != null)
{
collider.enabled = true;
}
TurnOnRecursively(child);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment