Search Unity

Line render with arrow at end

Discussion in '2D' started by adaptivemobiledevelopment, Jan 20, 2020.

  1. adaptivemobiledevelopment

    adaptivemobiledevelopment

    Joined:
    Mar 20, 2018
    Posts:
    30
    I'm trying to create a line render with an arrow at the end without luck. I have tired this code that I found but it seems to crate giant arrows. I'm not opposed to attaching a sprite at the end but the objects are moveable so the sprite would need to rotate along with the line render as it moves. I'm looking to recreate the arrows from the game "Crafting Idle Clicker"



    I have tired this without luck. It seems to create an arrow head but not the line.

    Code (CSharp):
    1. using UnityEngine;
    2. [RequireComponent(typeof(LineRenderer))]
    3. public class LineRendererArrow : MonoBehaviour
    4. {
    5.      [Tooltip("The percent of the line that is consumed by the arrowhead")]
    6.      [Range(0, 1)]
    7.      public float PercentHead = 0.4f;
    8.      public Vector3 ArrowOrigin;
    9.      public Vector3 ArrowTarget;
    10.      private LineRenderer cachedLineRenderer;
    11.      void Start()
    12.      {
    13.          UpdateArrow();
    14.      }
    15.      private void OnValidate()
    16.      {
    17.          UpdateArrow();
    18.      }
    19.      [ContextMenu("UpdateArrow")]
    20.      void UpdateArrow()
    21.      {
    22.          if (cachedLineRenderer == null)
    23.              cachedLineRenderer = this.GetComponent<LineRenderer>();
    24.          cachedLineRenderer.widthCurve = new AnimationCurve(
    25.              new Keyframe(0, 0.4f)
    26.              , new Keyframe(0.999f - PercentHead, 0.4f)  // neck of arrow
    27.              , new Keyframe(1 - PercentHead, 1f)  // max width of arrow head
    28.              , new Keyframe(1, 0f));  // tip of arrow
    29.          cachedLineRenderer.SetPositions(new Vector3[] {
    30.               ArrowOrigin
    31.               , Vector3.Lerp(ArrowOrigin, ArrowTarget, 0.999f - PercentHead)
    32.               , Vector3.Lerp(ArrowOrigin, ArrowTarget, 1 - PercentHead)
    33.               , ArrowTarget });
    34.      }
    35. }
     

    Attached Files: