Advertisement
vitareinforce

Deteksi Jarak

Apr 10th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Vuforia;
  6.  
  7. public class WeldingOnOff : MonoBehaviour {
  8.  
  9. // Initialize Welding Button
  10. public Button WeldingBtn;
  11.  
  12. // Initialize Spark Object
  13. public GameObject Spark;
  14. public AudioSource SparkSound;
  15.  
  16. // Initialize Fire Object
  17. public GameObject Fire;
  18.  
  19. public Text Guide;
  20. public AudioSource Selesai;
  21.  
  22. // Mode
  23. //public GameObject TwoF;
  24. //public GameObject TwoG;
  25. //public string mode;
  26.  
  27. // Initialize Trigger
  28. public bool WeldingOn;
  29.  
  30. // Setup range limit
  31. public float range = 1f;
  32.  
  33. // Setup raycast hit
  34. RaycastHit hit;
  35.  
  36. // Camera
  37. public Camera camera;
  38.  
  39.  
  40. void OnEnable()
  41. {
  42. // Register Button Events
  43. WeldingBtn.onClick.AddListener(() => buttonCallBack(WeldingBtn));
  44.  
  45. }
  46.  
  47. private void buttonCallBack(Button buttonPressed)
  48. {
  49. if (buttonPressed == WeldingBtn)
  50. {
  51. WeldingOn = !WeldingOn;
  52. }
  53. }
  54.  
  55. void OnDisable()
  56. {
  57. // Un-Register Button Events
  58. WeldingBtn.onClick.RemoveAllListeners();
  59. }
  60.  
  61. // Start is called before the first frame update
  62. void Start()
  63. {
  64. Spark.SetActive(false);
  65. Fire.SetActive(false);
  66. }
  67.  
  68. // Update is called once per frame
  69. void Update()
  70. {
  71. if (WeldingOn)
  72. {
  73. Fire.SetActive(true);
  74.  
  75. // Get the Vuforia StateManager
  76. StateManager sm = TrackerManager.Instance.GetStateManager();
  77.  
  78. // Query the StateManager to retrieve the list of
  79. // currently 'active' trackables
  80. //(i.e. the ones currently being tracked by Vuforia)
  81. IEnumerable<TrackableBehaviour> activeTrackables = sm.GetActiveTrackableBehaviours();
  82.  
  83. foreach (TrackableBehaviour tb in activeTrackables)
  84. {
  85. var jarak = Vector3.Distance(camera.transform.position, tb.gameObject.transform.position);
  86. Debug.Log("Jarak:" + jarak);
  87. Debug.Log("X:" + (camera.transform.position.x - tb.gameObject.transform.position.x));
  88. Debug.Log("Y:" + (camera.transform.position.y - tb.gameObject.transform.position.y));
  89. Debug.Log("Z:" + (camera.transform.position.z - tb.gameObject.transform.position.z));
  90.  
  91. if (tb.name == "Marker2F")
  92. {
  93. //detek jarak 2f
  94. if (jarak <= 450)
  95. {
  96. Spark.SetActive(true);
  97. SparkSound.Play();
  98. }
  99. else if (jarak >= 450)
  100. {
  101. Spark.SetActive(false);
  102. SparkSound.Stop();
  103. Guide.text = "Pengelasan 2F Selesai";
  104. Selesai.Play();
  105. Selesai.Stop();
  106. }
  107. else
  108. {
  109. Spark.SetActive(false);
  110. SparkSound.Stop();
  111. }
  112. }
  113.  
  114. if(tb.name == "Marker2G")
  115. {
  116. //detek jarak 2g
  117. if ((jarak >= 192) && (jarak <= 270))
  118. {
  119. Spark.SetActive(true);
  120. SparkSound.Play();
  121. }
  122. else if (jarak >= 270)
  123. {
  124. Spark.SetActive(false);
  125. SparkSound.Stop();
  126. Guide.text = "Pengelasan 2G Selesai";
  127. Selesai.Play();
  128. Selesai.Stop();
  129. }
  130. else
  131. {
  132. Spark.SetActive(false);
  133. SparkSound.Stop();
  134. }
  135.  
  136. }
  137. }
  138. }
  139. else
  140. {
  141. Spark.SetActive(false);
  142. Fire.SetActive(false);
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement