using UnityEngine; public class MoveProjectile : MonoBehaviour { public float movementSpeed; public Transform startingPos; public Transform endingPos; private float startTime; private float journeyLength; public GameObject explosionParticles; public GameObject animatedProjectile; private GameObject lastProjectileAnim; private AudioSource aSource; private void Start() { startTime = Time.time; journeyLength = Vector3.Distance(startingPos.position, endingPos.position); aSource = GetComponent<AudioSource>(); } private void Update() { // Distance moved = time * speed. float distCovered = (Time.time - startTime) * movementSpeed; // Fraction of journey completed = current distance divided by total distance. float fracJourney = distCovered / journeyLength; // Set our position as a fraction of the distance between the markers. transform.position = Vector3.Lerp(startingPos.position, endingPos.position, fracJourney); if (transform.position == endingPos.position) { Destroy(lastProjectileAnim); transform.position = startingPos.position; Instantiate(explosionParticles as GameObject, endingPos); lastProjectileAnim = Instantiate(animatedProjectile as GameObject, endingPos); startTime = Time.time; transform.parent.gameObject.GetComponent<Animator>().Play("turret Fire"); aSource.Play(); } } }