Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by AndyHuSen · Sep 27, 2016 at 12:10 PM · unity 2d

Unity2D , LineRenderer draw a line,than I Want to add an object on the center position of the line.what can i do ?

In Unity2D , Use LineRenderer draw a line,than I Want to add an object on the center position of the line.what can i do ?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bioinformatizer · Sep 27, 2016 at 12:43 PM

The center position of a line is Halfway in the line.

To get the line distance itself create a new Vector2 equal to the first point you use for LineRenderer and add the second point on a X and Y basis then divid by two.

Vector2 midpoint = new Vector2 ( (pointOne.x + pointTwo.x) / 2 , (pointOne.y + pointTwo.y) / 2);

Instantiate (newObject, midpoint, newRotation);

Good Luck!

Comment
Add comment · Show 4 · 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 Owen-Reynolds · Sep 27, 2016 at 06:18 PM 0
Share

Or just (pointOne+pointTwo)/2. That's standard vector math, which Unity supports.

(no comment about whether the rest will work.)

avatar image AndyHuSen · Sep 28, 2016 at 07:18 AM 0
Share

In Unity2D, these pictures are my project's interface , i want to add the string "hello" of the text on the line middle, but is not reach my target. as follows is my code. I do not know what is wrong about it ,please see the pictures and codes, give me an help, thanks

Codes:

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class NewBehaviourScript : $$anonymous$$onoBehaviour {

 public static Vector3 startPointOfLine;
 public static Vector3 endPointOfLine;
 public static bool flag = false;
 public $$anonymous$$aterial mat;
 public GameObject obj;
 // Use this for initialization
 void Start () {

 }
 
 // Update is called once per frame
 void Update () {
 
     if (Input.Get$$anonymous$$ouseButtonDown (0)) {

         flag = true;

         startPointOfLine = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,1.0f));

     }  
     if (Input.Get$$anonymous$$ouseButtonUp (0) && flag == true) {

         endPointOfLine = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,1.0f));;
         if (startPointOfLine != endPointOfLine) {
             SetLineRenderOnObj ();
             flag = false;
         }
     }

 }
 //ADD lineRender On gameobj上
 public void SetLineRenderOnObj(){
     obj = new GameObject();
     GameObject UIRoot = GameObject.FindWithTag ("UIRoot");
     obj.transform.SetParent (UIRoot.transform, false);
     LineRenderer $$anonymous$$ylineRenderer  = obj.AddComponent<LineRenderer>();
     $$anonymous$$ylineRenderer.SetColors (Color.yellow,Color.yellow);
     $$anonymous$$ylineRenderer.SetWidth (0.05f,0.05f);
     $$anonymous$$ylineRenderer.useWorldSpace = true;
     $$anonymous$$ylineRenderer.material = mat;
     $$anonymous$$ylineRenderer.SetPosition (0,startPointOfLine);
     $$anonymous$$ylineRenderer.SetPosition (1,endPointOfLine);

     //动态添加 ADD Text 
     GameObject textPrefab = Resources.Load ("Text") as GameObject;
     if (textPrefab != null) {
         Vector3 huaPosition = (startPointOfLine + endPointOfLine)/2;
         GameObject text = Instantiate (textPrefab,huaPosition,transform.rotation)as GameObject;
         text.GetComponent<Text>().text = "hello";
         text.transform.localScale = new Vector3 (1.0f,1.0f,1.0f);
         text.transform.Rotate(new Vector3(0, 0,0));  
         text.transform.SetParent(UIRoot.transform,false);  
     }

 }

}

avatar image Bioinformatizer AndyHuSen · Sep 28, 2016 at 03:46 PM 0
Share

-This is a test reply because I have not been able to submit the other code all morning-

avatar image Bioinformatizer Bioinformatizer · Sep 28, 2016 at 03:54 PM 0
Share

I have been trying to post the answer to this question for a while now but it will not let me paste the answer and submit it.

You have to bring the object back the screen frame of reference with WorldToScreenPoint from the camera function, just like you used it to find the ScreenToWorldPoint to begin with.

Right after your if statement try this :

 Vector3 huaPosition = (startPointOfLine + endPointOfLine) / 2;
                 Debug.Log("hua position before WorldToScreenPoint: " + huaPosition.x + "  " + huaPosition.y + "  " + huaPosition.z);
                 // THIS $$anonymous$$ETHOD BELOW 
                 huaPosition = Camera.main.WorldToScreenPoint(huaPosition);
                 huaPosition = new Vector3(huaPosition.x - Camera.main.pixelWidth/2, huaPosition.y - Camera.main.pixelHeight/2, 1.0f);
                 Debug.Log("hua position AFTER WorldToScreenPoint: " + huaPosition.x + "  " + huaPosition.y + "  " + huaPosition.z);
                 GameObject text = Instantiate(textPrefab, huaPosition, transform.rotation) as GameObject;
                 text.GetComponent<Text>().text = "hello";
                 text.GetComponent<Text>().verticalOverflow = VerticalWrap$$anonymous$$ode.Overflow;
                 text.GetComponent<Text>().horizontalOverflow = HorizontalWrap$$anonymous$$ode.Overflow;
                 text.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
                 text.transform.Rotate(new Vector3(0, 0, 0));
                 text.transform.SetParent(UIRoot.transform, false);


This should work out if your camera and canvas are both set to render as part of screen space and not world space. Alternatively you could just use a tiny WorldSpace canvas with the text attached as the prefab.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Particle System not working on multiple enemies 0 Answers

Buttons Not Working? PLEASE HELP!!!!!!!!!!!! 0 Answers

To use touch & raycasting to *drag and drop* a game object... 1 Answer

script causes unity to crash 0 Answers

Trying to build in android but, missing architecture??? Cannot upload to PlayStore without it 0 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