using System.Collections; using System.Collections.Generic; using UnityEngine; public class GrapeProjectile : MonoBehaviour { [SerializeField] private float duration = 1f; // Duration of the projectile's flight [SerializeField] private AnimationCurve animCurve; // Curve defining the projectile's arc [SerializeField] private float heightY = 3f; // Maximum height of the projectile's arc [SerializeField] private GameObject grapeProjectileShadow; // Shadow of the projectile [SerializeField] private GameObject splatterPrefab; // Prefab to instantiate on impact private void Start() { // Instantiate and position the shadow below the projectile GameObject grapeShadow = Instantiate(grapeProjectileShadow, transform.position + new Vector3(0, -0.3f, 0), Quaternion.identity); // Get the player's position and shadow's start position Vector3 playerPos = PlayerController.Instance.transform.position; Vector3 grapeShadowStartPosition = grapeShadow.transform.position; // Start the projectile's arc movement StartCoroutine(ProjectileCurveRoutine(transform.position, playerPos)); // Start the shadow's movement StartCoroutine(MoveGrapeShadowRoutine(grapeShadow, grapeShadowStartPosition, playerPos)); } // Controls the projectile's arc movement private IEnumerator ProjectileCurveRoutine(Vector3 startPosition, Vector3 endPosition) { float timePassed = 0f; while (timePassed < duration) { timePassed += Time.deltaTime; float linearT = timePassed / duration; float heightT = animCurve.Evaluate(linearT); float height = Mathf.Lerp(0f, heightY, heightT); // Update the projectile's position along the curve transform.position = Vector2.Lerp(startPosition, endPosition, linearT) + new Vector2(0f, height); yield return null; } // Spawn splatter effect on impact and destroy the projectile Instantiate(splatterPrefab, transform.position, Quaternion.identity); Destroy(gameObject); } // Controls the shadow's movement on the ground private IEnumerator MoveGrapeShadowRoutine(GameObject grapeShadow, Vector3 startPosition, Vector3 endPosition) { float timePassed = 0f; while (timePassed < duration) { timePassed += Time.deltaTime; float linearT = timePassed / duration; grapeShadow.transform.position = Vector2.Lerp(startPosition, endPosition, linearT); yield return null; } // Destroy the shadow after the projectile lands Destroy(grapeShadow); } }