Projectile.cs 701 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using UnityEngine;
  2. public class Projectile : MonoBehaviour
  3. {
  4. public float speed = 1.0f;
  5. public float maxLifeTime = 100f;
  6. private float currentLifeTime = 0f;
  7. // Start is called once before the first execution of Update after the MonoBehaviour is created
  8. void Start()
  9. {
  10. }
  11. // Update is called once per frame
  12. void Update()
  13. {
  14. Vector3 direction = transform.forward;
  15. transform.position = transform.position + (direction.normalized * Time.deltaTime * speed);
  16. currentLifeTime += Time.deltaTime;
  17. if (currentLifeTime >= maxLifeTime)
  18. {
  19. Destroy(gameObject);
  20. }
  21. }
  22. private void OnCollisionEnter(Collision collision)
  23. {
  24. Destroy(gameObject);
  25. }
  26. }