Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rozebau · Nov 19, 2015 at 11:44 AM · c#2dlinerendererdrawmousedrag

Making a arrow instead of Linerenderer

Hi all,

I'm trying to create something like an 2D Curling Game, so far i have the curling physics almost down but i'm having difficulties in figuring out how to draw a nice indicator of the force used to hit the curling (MouseDrag).

Currently i have a LineRenderer that is drawn on MouseDrag event, it looks like this:

alt text

It looks ok but i instead of this LineRenderer i would like to have an arrow drawn in the size of the drag size or the force used to push the curling ball as well as the direction, something similar to the arrows drawn in a game called soccer stars, something like this:

alt text

Can please somebody help me figure this out, here is also the code i have written so far:

 using UnityEngine;
 using System.Collections;
 
 public class Controller : MonoBehaviour
 {
     public float start_x, start_y, end_x, end_y, force_x, force_y;
     public int force = 60;
     //public GameObject arrow;
     
     
     
     //Taking the initial position of the mouse
     void OnMouseDown()
     {
         start_x = GameObject.Find("Puck").transform.position.x;
         start_y = GameObject.Find("Puck").transform.position.y;
 
         //arrow = Instantiate(Resources.Load("Arrow"),new Vector3(start_x,start_y,1f),Quaternion.identity) as GameObject;
         
         
     }
     
     
     
     //Taking the end position of the mouse and drawing a line renderer
     void OnMouseDrag()
     {
         GameObject.Find("Line").transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         GameObject.Find("Line").transform.position = new Vector3(GameObject.Find("Line").transform.position.x, GameObject.Find("Line").transform.position.y, 0);
         
         end_x = GameObject.Find("Line").transform.position.x;
         end_y = GameObject.Find("Line").transform.position.y;
 
         //arrow.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         //arrow.transform.position = new Vector3(GameObject.Find("Line").transform.position.x, GameObject.Find("Line").transform.position.y, 0);
         //arrow.transform.rotation = GameObject.Find("Line").transform.rotation;
         
 
         if(GetComponent<LineRenderer>() != null)
         {
             GetComponent<LineRenderer>().enabled = true;
             GetComponent<LineRenderer>().SetVertexCount(2);
             GetComponent<LineRenderer>().SetPosition(0, new Vector3(transform.position.x, transform.position.y, 1));
             GetComponent<LineRenderer>().SetPosition(1, new Vector3(GameObject.Find("Line").transform.position.x, GameObject.Find("Line").transform.position.y, 1));
             GetComponent<LineRenderer>().SetWidth(0f, 1f);
             GetComponent<LineRenderer>().sortingLayerName = ("myLayer");
             GetComponent<LineRenderer>().sortingOrder = 10;
         }
     }
     
     
     //Calculating the force
     void OnMouseUp()
     {
         force_x = start_x - end_x;
         force_y = start_y - end_y;
         
         GetComponent<Rigidbody2D>().AddForce(Vector2.up * force_y * force);
         GetComponent<Rigidbody2D>().AddForce(Vector2.right * force_x * force);
         
             
         //Hide LineRenderer if one is attached
         if(GetComponent<LineRenderer>() != null)
         {
             GetComponent<LineRenderer>().enabled = false;
         }
 
         //Destroy(arrow);
         
         //Turning on gravity if needed
         //GetComponent<Rigidbody2D>().gravityScale = 1;
     }
 
     void Update()
     {
         GetComponent<Rigidbody2D>().drag = 0.9f;
     }
 
     
     
 }
 

Thank you in forward.

myexamplefw.png (63.1 kB)
soccerstars-examplefw.png (271.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by ShawnFeatherly · Mar 23, 2017 at 06:17 PM

With Unity3D 5.5 you can use the WidthCurve of the LineRenderer to make arrows. Be careful to place the line positions to match that width curve. Otherwise the mesh won't be generated correctly. Here's a script:

 using UnityEngine;

 [RequireComponent(typeof(LineRenderer))]
 public class LineRendererArrow : MonoBehaviour
 {
     [Tooltip("The percent of the line that is consumed by the arrowhead")]
     [Range(0, 1)]
     public float PercentHead = 0.4f;
     public Vector3 ArrowOrigin;
     public Vector3 ArrowTarget;
     private LineRenderer cachedLineRenderer;

     void Start()
     {
         UpdateArrow();
     }

     private void OnValidate()
     {
         UpdateArrow();
     }

     [ContextMenu("UpdateArrow")]
     void UpdateArrow()
     {
         if (cachedLineRenderer == null)
             cachedLineRenderer = this.GetComponent<LineRenderer>();

         cachedLineRenderer.widthCurve = new AnimationCurve(
             new Keyframe(0, 0.4f)
             , new Keyframe(0.999f - PercentHead, 0.4f)  // neck of arrow
             , new Keyframe(1 - PercentHead, 1f)  // max width of arrow head
             , new Keyframe(1, 0f));  // tip of arrow
         cachedLineRenderer.SetPositions(new Vector3[] {
              ArrowOrigin
              , Vector3.Lerp(ArrowOrigin, ArrowTarget, 0.999f - PercentHead)
              , Vector3.Lerp(ArrowOrigin, ArrowTarget, 1 - PercentHead)
              , ArrowTarget });
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tillmonkthered · Jul 23, 2018 at 07:16 PM 1
Share

This is a great solution, to which I would suggest the following tweak.

 void UpdateArrow()
 {
     **float AdaptiveSize = (float)(PercentHead / Vector3.Distance(ArrowOrigin, ArrowTarget));**

     if (cachedLineRenderer == null)
         cachedLineRenderer = this.GetComponent<LineRenderer>();
     cachedLineRenderer.widthCurve = new AnimationCurve(
         new $$anonymous$$eyframe(0, 0.4f)
         , new $$anonymous$$eyframe(0.999f - **AdaptiveSize**, 0.4f)  // neck of arrow
         , new $$anonymous$$eyframe(1 - **AdaptiveSize**, 1f)  // max width of arrow head
         , new $$anonymous$$eyframe(1, 0f));  // tip of arrow
     cachedLineRenderer.SetPositions(new Vector3[] {
               ArrowOrigin
               , Vector3.Lerp(ArrowOrigin, ArrowTarget, 0.999f - **AdaptiveSize**)
               , Vector3.Lerp(ArrowOrigin, ArrowTarget, 1 - **AdaptiveSize**)
               , ArrowTarget });
 }

This will keep the head of the arrow the same absolute size (the head won't stretch/scale), even as the arrow lengthens/shortens. You'll just want to adjust the value of PercentHead until you get the arrowhead to be the size you want.

Furthermore, I ended up having to split SetPositions() code into four separate SetPosition() statements. But that's probably just because I'm new to Unity and made a mistake somewhere.

avatar image
1

Answer by _Game_Dev_Dude_ · Nov 19, 2015 at 03:42 PM

You will need to either find or create an arrow sprite of your own. Then position,scale and rotate it according to your input and controls. There is no built in unity way to draw an arrow other than the Handles library but this is used for custom editors and not game time drawing.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Can't get linerenderers to work... 0 Answers

Distribute terrain in zones 3 Answers

draw and compare 1 Answer

How to make Line Renderer lines stay flat? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges