Untitled

mail@pastecode.io avatar
unknown
csharp
11 days ago
3.4 kB
1
Indexable
Never
using UnityEngine;
using System.Collections.Generic;

public class Launcher : MonoBehaviour
{
    [SerializeField] private float upRange = 5;
    [SerializeField] private int resolution = 20;
    [SerializeField] private bool useParabolicCurve = true;
    [SerializeField] private LayerMask collisionMask;
    [SerializeField] private Transform endPoint;

    private List<Vector3> trajectoryPoints = new List<Vector3>();

    [SerializeField] private float launchDuration = 2.0f;

    void OnDrawGizmos()
    {
        if (useParabolicCurve)
        {
            DrawParabolicCurve();
        }
        else
        {
            DrawStraightLine();
        }
    }

    void DrawParabolicCurve()
    {
        Vector3 startPos = transform.position;
        Vector3 previousPos = startPos;

        trajectoryPoints.Clear();

        for (int i = 1; i <= resolution; i++)
        {
            float t = (float)i / resolution;
            Vector3 point = CalculateParabolaPoint(startPos, t, endPoint.position);
            trajectoryPoints.Add(point);

            
            Gizmos.color = Color.yellow; 
            Gizmos.DrawSphere(point, 0.1f); 

            if (Physics.Raycast(previousPos, point - previousPos, out RaycastHit hit, Vector3.Distance(previousPos, point), collisionMask))
            {
                Gizmos.color = Color.red;
                Gizmos.DrawLine(previousPos, hit.point);
                break;
            }

            Gizmos.color = Color.blue;
            Gizmos.DrawLine(previousPos, point);
            previousPos = point;
        }
    }

    void DrawStraightLine()
    {
        Vector3 startPos = transform.position;
        Vector3 endPos = endPoint.position;
        Vector3 previousPos = startPos;

        trajectoryPoints.Clear(); 

        
        for (int i = 1; i <= resolution; i++)
        {
            float t = (float)i / resolution;
            Vector3 point = Vector3.Lerp(startPos, endPos, t);  
            trajectoryPoints.Add(point);  

            Gizmos.color = Color.yellow;
            Gizmos.DrawSphere(point, 0.1f); 

            Gizmos.color = Color.blue;
            Gizmos.DrawLine(previousPos, point);  
            previousPos = point;
        }
    }

    Vector3 CalculateParabolaPoint(Vector3 startPos, float t, Vector3 endPos)
    {
        Vector3 horizontalPoint = Vector3.Lerp(startPos, endPos, t);
        float parabolaHeight = 4 * upRange * t * (1 - t);
        float verticalPoint = Mathf.Lerp(startPos.y, endPos.y, t) + parabolaHeight;
        return new Vector3(horizontalPoint.x, verticalPoint, horizontalPoint.z);
    }

    public List<Vector3> GetTrajectoryPoints()
    {
        return trajectoryPoints;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            MoveAction moveAction = other.transform.parent.Find("Actions/Move").GetComponent<MoveAction>();
            LaunchPlayer(moveAction);
        }
    }

    public void LaunchPlayer(MoveAction playerMoveAction)
    {
        if (playerMoveAction != null)
        {
            List<Vector3> trajectory = GetTrajectoryPoints();
            playerMoveAction.StartLaunch(trajectory.ToArray(), launchDuration);
        }
    }
}
Leave a Comment