Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Yharooer · Jul 14, 2013 at 10:13 AM · guionguiline

Draw an Arrow on the Screen

Hi,

I would like to draw an arrow on the screen, from one coordinate to another on the screen, so the coordinated are the screen coordinates (2D) rather that in the 3D world. This will change depending on where the player's finger is on the screen.

How can I make an arrow appear from one coordinate to another, preferably "dynamic", so the arrow can be longer without it being stretched?

Thanks!

Comment
Add comment · Show 1
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 Eric5h5 · Jul 14, 2013 at 11:53 PM 0
Share

Vectrosity would be perfect for that; it would only take a few lines of code.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Jul 14, 2013 at 03:45 PM

Here is a script that will position, rotate, and scale an object to extend between two game objects dynamically. Its current form uses the 'y' of the object, so the top and bottom are at the two specified positions. try it with build-in cylinder, built-in cube, or a 1 unit, vertical plane from the CreatePlane editor script. It only scales the 'Y' values, so the x, and z scaling can be set as appropriate for visual look. In order for the arrow to not look stretched, make the arrow head a separate object. Place it at the end point, rotate it, but don't scale it.

 pragma strict
 
 var pos1 : Vector3;
 var pos2 : Vector3;
 var objectHeight = 2.0;  // 2.0 for a cylinder, 1.0 for a cube
 
 function Update () {
 
     if (Input.GetMouseButtonDown(0)) {
         pos1 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
         pos1 = Camera.main.ScreenToWorldPoint(pos1); 
         pos2 = pos1;
     
     }
     
     if (Input.GetMouseButton(0)) {
         pos2 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
         pos2 = Camera.main.ScreenToWorldPoint(pos2); 
     
     }
     
     if (pos2 != pos1) {
         var v3 = pos2 - pos1;
         transform.position = pos1 + (v3) / 2.0;
         transform.localScale.y = v3.magnitude/2.0;
         transform.rotation = Quaternion.FromToRotation(Vector3.up, v3);
     }
 }

Comment
Add comment · Show 5 · 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 Yharooer · Jul 14, 2013 at 11:31 PM 0
Share

How can I make this so its between 2 points on the screen, ie not in the 3D space, but in the 2D GUI space?

So Like from (500,100) to (300,200) for example.

avatar image Yharooer · Jul 14, 2013 at 11:43 PM 0
Share

and Should it be function OnGUI() rather than function Update()?

avatar image robertbu · Jul 15, 2013 at 12:16 AM 1
Share

As @Eric5h5 mentions, Vectrosity is an excellent line drawing package you can find in the Asset store that makes this problem much easier. Note the code above should be in Update(). If you wanted to draw 2D using the code above and using mouse positions, then move the two positions used in the code above to the mouse position just beyond the near clip plane of the camera. That can be accomplished using Camera.ScreenToWorldPoint().

 var v3Pos = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.1);
 v3Pos = Camera.main.ScreenToWorldPoint(v3Pos); 

I've edited the code above so that you can drag out the object using the mouse.

avatar image Yharooer · Jul 15, 2013 at 12:25 AM 0
Share

Thanks, I'll see if it works later. :)

avatar image robertbu · Jul 15, 2013 at 03:24 AM 0
Share

The reedited code above was tested with a cylinder scaled to (0.005, 1.0, 0.005).

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

17 People are following this question.

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

Related Questions

Display a Line in GUI without texture - GUI Line ? 6 Answers

Free Gui solutions 2 Answers

Unity 4.7 - OnGUI prevent click/touch through 0 Answers

OnGUI button created by a foreach loop 4 Answers

how to render line between gui element and mouse position 1 Answer


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