Funny AR app
This is an Augmented reality (AR) game made using Unity and Vuforia.
Here, by targeting an image, the player sees a 2D fantasy alien attack game and can play with it.
Sample pictures
C# code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { Rigidbody2D playerRig; float _speed = 0; Animator boyAnimator; SpriteRenderer spriteRenderer; public float speed = 10; public static float playerXpos; // Start is called before the first frame update void Start() { playerRig = gameObject.GetComponent<Rigidbody2D>(); boyAnimator = GetComponentInChildren<Animator>(); spriteRenderer = GetComponentInChildren<SpriteRenderer>(); } private void Update() { playerXpos = transform.position.x; } private void FixedUpdate() { playerRig.velocity = new Vector2(_speed, playerRig.velocity.y); } public void RunR() { _speed = speed; boyAnimator.SetFloat("speed", speed); spriteRenderer.flipX = true; } public void RunL() { _speed = -speed; boyAnimator.SetFloat("speed", speed); spriteRenderer.flipX = false; } public void Stop() { _speed = 0; boyAnimator.SetFloat("speed", 0); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Alien : MonoBehaviour { Animator alienAnimator; public float speed = 2; float i = 0; // Start is called before the first frame update void Start() { alienAnimator = GetComponent<Animator>(); } private void Update() { transform.Translate(new Vector2(i * speed * Time.deltaTime, 0)); } private void OnCollisionStay2D(Collision2D collision) { if (collision.gameObject.tag == "Ground") { alienAnimator.SetBool("Run", true); } else if(collision.gameObject.tag == "Destroy") { Destroy(gameObject); } } public void StartRun() { i = (Player.playerXpos - transform.position.x) / Mathf.Abs(Player.playerXpos - transform.position.x); //Debug.Log(i); //transform.rotation = new Quaternion(0, Mathf.Acos(i), 0, 0); GetComponent<SpriteRenderer>().flipX = i < 0; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class InstantiateObjects : MonoBehaviour { public GameObject flyingSaucer; public GameObject[] alien; int r; // Start is called before the first frame update void Start() { StartCoroutine(Inst()); } // Update is called once per frame void Update() { r = Random.Range(0, alien.Length); } IEnumerator Inst() { yield return new WaitForSeconds(2f); flyingSaucer.SetActive(true); yield return new WaitForSeconds(1.5f); for(int i =0; i<10; i++) { Instantiate(alien[r],transform.position,transform.rotation); yield return new WaitForSeconds(5f); } flyingSaucer.SetActive(false); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | /*============================================================================== Copyright (c) 2017 PTC Inc. All Rights Reserved. Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc. All Rights Reserved. Confidential and Proprietary - Protected under copyright and other laws. ==============================================================================*/ using UnityEngine; using Vuforia; /// <summary> /// A custom handler that implements the ITrackableEventHandler interface. /// /// Changes made to this file could be overwritten when upgrading the Vuforia version. /// When implementing custom event handler behavior, consider inheriting from this class instead. /// </summary> public class MyTrackableEventHandler : MonoBehaviour, ITrackableEventHandler { #region PROTECTED_MEMBER_VARIABLES protected TrackableBehaviour mTrackableBehaviour; protected TrackableBehaviour.Status m_PreviousStatus; protected TrackableBehaviour.Status m_NewStatus; #endregion // PROTECTED_MEMBER_VARIABLES #region UNITY_MONOBEHAVIOUR_METHODS protected virtual void Start() { mTrackableBehaviour = GetComponent<TrackableBehaviour>(); if (mTrackableBehaviour) mTrackableBehaviour.RegisterTrackableEventHandler(this); } protected virtual void OnDestroy() { if (mTrackableBehaviour) mTrackableBehaviour.UnregisterTrackableEventHandler(this); } #endregion // UNITY_MONOBEHAVIOUR_METHODS #region PUBLIC_METHODS /// <summary> /// Implementation of the ITrackableEventHandler function called when the /// tracking state changes. /// </summary> public void OnTrackableStateChanged( TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus) { m_PreviousStatus = previousStatus; m_NewStatus = newStatus; if (newStatus == TrackableBehaviour.Status.DETECTED || newStatus == TrackableBehaviour.Status.TRACKED || newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED) { Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found"); OnTrackingFound(); } else if (previousStatus == TrackableBehaviour.Status.TRACKED && newStatus == TrackableBehaviour.Status.NO_POSE) { Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost"); OnTrackingLost(); } else { // For combo of previousStatus=UNKNOWN + newStatus=UNKNOWN|NOT_FOUND // Vuforia is starting, but tracking has not been lost or found yet // Call OnTrackingLost() to hide the augmentations OnTrackingLost(); } } #endregion // PUBLIC_METHODS #region PROTECTED_METHODS public GameObject obj; public Transform pos; GameObject _obj; protected virtual void OnTrackingFound() { _obj = Instantiate(obj, pos.position, pos.rotation); _obj.transform.parent = gameObject.transform; //var rendererComponents = GetComponentsInChildren<Renderer>(true); //var colliderComponents = GetComponentsInChildren<Collider>(true); //var canvasComponents = GetComponentsInChildren<Canvas>(true); //// Enable rendering: //foreach (var component in rendererComponents) // component.enabled = true; //// Enable colliders: //foreach (var component in colliderComponents) // component.enabled = true; //// Enable canvas': //foreach (var component in canvasComponents) // component.enabled = true; } protected virtual void OnTrackingLost() { Destroy(_obj); GameObject[] alienObj = GameObject.FindGameObjectsWithTag("alien"); foreach (GameObject alObj in alienObj) { Destroy(alObj); } //var rendererComponents = GetComponentsInChildren<Renderer>(true); //var colliderComponents = GetComponentsInChildren<Collider>(true); //var canvasComponents = GetComponentsInChildren<Canvas>(true); //// Disable rendering: //foreach (var component in rendererComponents) // component.enabled = false; //// Disable colliders: //foreach (var component in colliderComponents) // component.enabled = false; //// Disable canvas': //foreach (var component in canvasComponents) // component.enabled = false; } #endregion // PROTECTED_METHODS } |