ObjectShooter.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UnityEngine;
  2. using UnityARInterface;
  3. public class ObjectShooter : ARBase
  4. {
  5. [SerializeField]
  6. private GameObject m_Prefab;
  7. [SerializeField]
  8. private float m_Force = 5000f;
  9. public float minimumYValue = 0f;
  10. private bool m_WasFireRequested = false;
  11. private Vector2 m_ScreenPosition;
  12. public void RequestFire(Vector2 screenPosition)
  13. {
  14. m_WasFireRequested = true;
  15. m_ScreenPosition = screenPosition;
  16. }
  17. void Update()
  18. {
  19. if (m_WasFireRequested)
  20. {
  21. var camera = GetCamera();
  22. var ray = camera.ScreenPointToRay(m_ScreenPosition);
  23. var go = Instantiate(m_Prefab, ray.origin + ray.direction * 2f, Quaternion.identity);
  24. var rigidbody = go.GetComponent<Rigidbody>();
  25. if (rigidbody != null)
  26. {
  27. var force = ray.direction * m_Force;
  28. rigidbody.AddForce(force);
  29. }
  30. var remover = go.GetComponent<RemoveRigidbody>();
  31. remover.minYPosition = minimumYValue;
  32. }
  33. m_WasFireRequested = false;
  34. }
  35. }