trajectory line

mail@pastecode.io avatar
unknown
csharp
5 months ago
3.3 kB
2
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TrajectoryLine : MonoBehaviour
{
    [Header("Trajectory Line Smoothness/Length")]
    [SerializeField] private int _segmentCount = 50;
    [SerializeField] private float _teleportRange = 5f; // The maximum distance the player can teleport

    private Vector2[] _segments;
    private LineRenderer _lineRenderer;
    public Teleport _teleport;

    private void Start()
    {
        // Initialize segments
        _segments = new Vector2[_segmentCount];

        _lineRenderer = GetComponent<LineRenderer>();
        if (_lineRenderer == null)
        {
            Debug.LogError("LineRenderer component not found!");
            enabled = false;
            return;
        }
        _lineRenderer.positionCount = _segmentCount;
    }

    private void Update()
    {
        if (_teleport == null || Time.time < _teleport.nextTeleportTime)
        {
            // Hide the line renderer if cooldown is active
            _lineRenderer.enabled = false;
            return;
        }
        else
        {
            _lineRenderer.enabled = true;
        }

        if (Input.GetMouseButton(0))
        {
            DrawTrajectory();
        }
        else
        {
            // Hide the line when the mouse button is released
            _lineRenderer.positionCount = 0;
        }
    }

    private void DrawTrajectory()
    {
        Vector3 startPos = (Vector2)transform.position;
        Vector3 mousePos = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
        mousePos.z = 0; // Ensure the z-coordinate is zero to match 2D context
        Vector3 direction = (mousePos - startPos).normalized;
        float distanceToMouse = Vector2.Distance(startPos, mousePos);

        // Determine the number of segments to use based on distance to the mouse
        float segmentStep = _teleportRange / (_segmentCount - 1); // Distance between segments
        int segmentsCount = Mathf.CeilToInt(distanceToMouse / segmentStep) + 1;

        // Adjust segment count if mouse is within range
        if (distanceToMouse > _teleportRange)
        {
            segmentsCount = _segmentCount;
        }

        for (int i = 0; i < segmentsCount; i++)
        {
            Vector3 position;
            if (distanceToMouse <= _teleportRange)
            {
                position = startPos + direction * segmentStep * i;
                if (Vector2.Distance(startPos, position) > distanceToMouse)
                {
                    position = mousePos;
                }
            }
            else
            {
                position = startPos + direction * segmentStep * i;
                if (Vector2.Distance(startPos, position) > _teleportRange)
                {
                    _lineRenderer.positionCount = i;
                    break;
                }
            }
            _segments[i] = position;
        }

        // Update the LineRenderer
        _lineRenderer.positionCount = segmentsCount;
        Vector3[] positions = new Vector3[segmentsCount];
        for (int i = 0; i < segmentsCount; i++)
        {
            positions[i] = new Vector3(_segments[i].x, _segments[i].y, 0);
        }
        _lineRenderer.SetPositions(positions);
    }
}
Leave a Comment