Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Use line renderer to create an arrow

Discussion in 'Scripting' started by Mari9917, May 12, 2023.

  1. Mari9917

    Mari9917

    Joined:
    Apr 6, 2023
    Posts:
    4
    I am trying to attach an arrow that changes direction and color to a ball in my minigolf game, using line renderer. I tried to give the shape of an arrow but I can't do it. I would like to write a script that allow the arrow to rotate in the direction chosen by the user with some buttons and to change color based on the force chosen, but I can't even start by creating the arrow. Should I change method? Should I use something else other than line renderer?
    Thanks in advance
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,630
    You don't mention what's the problem preventing you from drawing an arrow. Just placing the points in the line to form an arrow shape should be enough?

    If you're unsure about the math required to do this, I'd recommend looking up basic vector math, specifically addition, normalization, dot and cross product. Those should be enough for your purpose.
     
    Bunny83 likes this.
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,955
    if the arrow is always same size and length,
    probably easier to use pre-made 2D or 3D arrow sprite/model. (and can adjust tint color in the material)

    do you have some inspiration/reference image from other minigolf games?
    some of them have animated arrows too.
     
  4. Mari9917

    Mari9917

    Joined:
    Apr 6, 2023
    Posts:
    4
    Sorry but I am very new at using Unity and I am learning from various tutorials... do you mean that I have to use these operations in a script attached to the object with the line renderer? I have no idea how to do that, but maybe I have to read again the line renderer manual
     
  5. Mari9917

    Mari9917

    Joined:
    Apr 6, 2023
    Posts:
    4
    I would like it to change also in dimensions, something similar to infiniteMinigolf for example
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,955
    scaling could work,
    or having the arrow head as separate object, so can scale line length only.

    screenshot from that game,
    could also have that kind of custom shader that fills or reveals the gradient.
    upload_2023-5-12_17-29-14.png
     
  7. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,630
    Correct, you use these operations to calculate the points that describe an arrow shape, then pass these to the line renderer.

    Note that using a LineRenderer is only good if you want the arrow to be an outline, otherwise you’d want to use another method. Here’s a video tutorial on how to draw an arrow using a mesh:

     
    Kurt-Dekker likes this.
  8. Mari9917

    Mari9917

    Joined:
    Apr 6, 2023
    Posts:
    4
    Thank you very much!!