Last active
December 18, 2017 11:16
-
-
Save tarukosu/26d65b8e5fb917760e2a92cd4131a235 to your computer and use it in GitHub Desktop.
GunScript.cs
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
using UnityEngine; | |
using Vuforia; | |
public class GunScript : MonoBehaviour { | |
public ITangoController TangoController; | |
public GameObject LaserBeam; | |
public CustomTrackingHandler trackingHandler; | |
bool previousSelectButton = false; | |
//Tracking | |
bool isTracking = false; | |
float startTrackingTime = 0f; | |
const float START_CALIBRATION_TIME = 1f; | |
const float CALIBRATE_TIME = 1f; | |
private GameObject muzzle; | |
void Start () { | |
muzzle = transform.Find("Muzzle").gameObject; | |
muzzle.transform.localPosition = new Vector3(0f, -0.1f, 0.2f); | |
muzzle.transform.localRotation = Quaternion.Euler(5f, 0f, 0f); | |
trackingHandler.OnTrackingStateChanged += OnTrackingStateChanged; | |
trackingHandler.OnTracking += OnTracking; | |
} | |
private void OnTrackingStateChanged(TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus) | |
{ | |
if(newStatus == TrackableBehaviour.Status.TRACKED) | |
{ | |
Debug.Log("tracking started"); | |
startTrackingTime = Time.time; | |
isTracking = true; | |
} | |
else | |
{ | |
isTracking = false; | |
} | |
} | |
private void OnTracking(Transform obj) | |
{ | |
if (isTracking) | |
{ | |
var now = Time.time; | |
if(now < startTrackingTime + START_CALIBRATION_TIME) | |
{ | |
return; | |
} | |
if (now < startTrackingTime + START_CALIBRATION_TIME + CALIBRATE_TIME) | |
{ | |
if(TangoController == null) | |
{ | |
return; | |
} | |
var originToMarker = Matrix4x4.TRS(obj.position, obj.rotation * Quaternion.Euler(90, 0, 0), Vector3.one); | |
var tangoOriginToMarker = Matrix4x4.TRS(TangoController.transform.position, TangoController.transform.rotation, Vector3.one); | |
var originToTangoOrigin = originToMarker * tangoOriginToMarker.inverse; | |
var rot = GetRotationFromMatrix(originToTangoOrigin); | |
transform.parent.SetPositionAndRotation(originToTangoOrigin.MultiplyPoint(Vector3.zero), rot); | |
} | |
} | |
} | |
Quaternion GetRotationFromMatrix(Matrix4x4 matrix) | |
{ | |
return Quaternion.LookRotation(matrix.GetColumn(2), matrix.GetColumn(1)); | |
} | |
void Update () { | |
if(TangoController == null) | |
{ | |
return; | |
} | |
var t = 0.8f; | |
transform.localPosition = Vector3.Lerp(transform.localPosition, TangoController.transform.position, t); | |
transform.localRotation = Quaternion.Lerp(transform.localRotation, TangoController.transform.rotation, t); | |
// 発射 | |
if (previousSelectButton == false && TangoController.SelectButton) | |
{ | |
var beam = Instantiate(LaserBeam, muzzle.transform.position, muzzle.transform.rotation); | |
} | |
previousSelectButton = TangoController.SelectButton; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment