LaunchProjectile.cs 1023 B

123456789101112131415161718192021222324252627282930313233343536
  1. using UnityEngine;
  2. namespace Unity.VRTemplate
  3. {
  4. /// <summary>
  5. /// Apply forward force to instantiated prefab
  6. /// </summary>
  7. public class LaunchProjectile : MonoBehaviour
  8. {
  9. [SerializeField]
  10. [Tooltip("The projectile that's created")]
  11. GameObject m_ProjectilePrefab = null;
  12. [SerializeField]
  13. [Tooltip("The point that the project is created")]
  14. Transform m_StartPoint = null;
  15. [SerializeField]
  16. [Tooltip("The speed at which the projectile is launched")]
  17. float m_LaunchSpeed = 1.0f;
  18. public void Fire()
  19. {
  20. GameObject newObject = Instantiate(m_ProjectilePrefab, m_StartPoint.position, m_StartPoint.rotation, null);
  21. if (newObject.TryGetComponent(out Rigidbody rigidBody))
  22. ApplyForce(rigidBody);
  23. }
  24. void ApplyForce(Rigidbody rigidBody)
  25. {
  26. Vector3 force = m_StartPoint.forward * m_LaunchSpeed;
  27. rigidBody.AddForce(force);
  28. }
  29. }
  30. }