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 ?
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!
Or just (pointOne+pointTwo)/2. That's standard vector math, which Unity supports.
(no comment about whether the rest will work.)
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);
}
}
}
-This is a test reply because I have not been able to submit the other code all morning-
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
Follow this Question
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