Projectile.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Projectile : MonoBehaviour
  5. {
  6. private float disappearAfterTime = 3;
  7. private float disappearTimer = 0;
  8. private bool isActive = false;
  9. // private MeshRenderer meshRenderer;
  10. private Vector3 direction;
  11. private Vector3 startPosition;
  12. private float movementSpeed = 1;
  13. private Vector3 targetPosition;
  14. private float distanceToTarget;
  15. private float movementValue;
  16. // Use this for initialization
  17. void Start ()
  18. {
  19. // We cache this for performance reasons, GetComponent is slow to do realtime
  20. // meshRenderer = GetComponent<MeshRenderer>();
  21. // meshRenderer = GetComponent<MeshRenderer>();
  22. }
  23. // Update is called once per frame
  24. void Update () {
  25. if (isActive) // Only update stuff if we're alive
  26. {
  27. disappearTimer += Time.deltaTime; // Increase disappear timer
  28. if (disappearTimer > disappearAfterTime) // If we're alive too long, get rekt
  29. {
  30. disappearTimer = 0; // Reset timer
  31. isActive = false; // Is not active anymore
  32. // meshRenderer.enabled = false; // Disable meshrender so it's invisible
  33. }
  34. // 1/distanceToTarget is the calculation to move 1 unit per second, movementspeed defines how many units per second you want to move
  35. movementValue += (1/distanceToTarget*movementSpeed) * Time.deltaTime;
  36. if (movementValue > 1)
  37. {
  38. movementValue = 1;
  39. Explode();
  40. }
  41. Move();
  42. }
  43. }
  44. void Move()
  45. {
  46. // lerp goes from 0 to 1, 0 is startPosition, 1 is the targets position;
  47. transform.position = Vector3.Lerp(startPosition, targetPosition, movementValue);
  48. }
  49. void MoveWithoutTargetHit()
  50. {
  51. transform.position += direction.normalized * movementSpeed;
  52. }
  53. public void Fire(Vector3 target, Vector3 spawnPosition, Vector3 Direction, float speed)
  54. {
  55. if (isActive) // If we're active, just return so we don't execute any code
  56. return;
  57. isActive = true; // Set active
  58. disappearTimer = 0; // Reset timer just in case it's not reset
  59. transform.position = spawnPosition; // set spawn position
  60. // meshRenderer.enabled = true; // Enable meshrender so it's visible
  61. movementSpeed = speed; // Units per second
  62. direction = Direction.normalized; // Normalize the direction
  63. targetPosition = target; // Set target transform - Can be null for continous movement without target
  64. distanceToTarget = Vector3.Distance(targetPosition, transform.position);
  65. startPosition = spawnPosition;
  66. movementValue = 0;
  67. }
  68. // It gets here after it hits something
  69. void Explode()
  70. {
  71. disappearTimer = 0; // Reset timer
  72. isActive = false; // Is not active anymore
  73. // meshRenderer.enabled = false; // Disable meshrender so it's invisible
  74. }
  75. public bool GetIsActive()
  76. {
  77. return isActive;
  78. }
  79. }