# How to render text along the line?

Hi, I want to render a text along on a line. I have done something like this.

``````measureLine.SetPosition(0, start.transform.position);
measureLine.SetPosition(1, end.transform.position);
Vector3 point = (start.transform.position + end.transform.position) / 2;
point.y += 0.02f;
Vector3 dir = end.transform.position - start.transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg -90;
var measurement = Vector3.Distance(start.transform.position, end.transform.position) * measurementFactor;
textObj.transform.eulerAngles = Vector3.forward *angle;
``````

But this doesnâ€™t give the output I want. I need something like below. Text should be rendered on the line.

I donâ€™t see what you mean by that picture. For me this text is on the line.

edit:
ah sorry that was the example of what youâ€™d like to see, didnâ€™t get you.

if you have two points in space, you can find the direction vector of that segment quite easily.
thatâ€™s `(p2 - p1).normalized`

a mid point on the segment would be `(p1 + p2) / 2f`

and depending on the initial orientation of the text (letâ€™s assume itâ€™s coplanar with XY)
the rotation can be found with
`Quaternion.FromToRotation(Vector3.right, (p2 - p1).normalized);`

if your original textâ€™s pivot is left-bottom aligned, you can move it either before this transformation (easier), or after (you need to transform offset as well)

the transformation itself should be

``````text.position = (p1 + p2) / 2f + offset; // offset can be zero if the text was centered
text.rotation = Quaternion.FromToRotation(Vector3.right, (p2 - p1).normalized);
``````

people also like to use transform.LookRotation so check that out as well
I think itâ€™s not that useful for situations like this one but you can have some pseudo-control over the lateral rotation

edit:
what do I mean by this? well if your original text was coplanar with XZ plane, the previous code will work but the text will be rotated by 90 degrees on X.

you can fix this rotation manually if you do this instead

``````text.rotation = Quaternion.Euler(anyAngle, 0f, 0f) * Quaternion.FromToRotation(Vector3.right, (p2 - p1).normalized);
``````

if you donâ€™t want this type of control, then LookRotation is very likely the best option

1 Like

donâ€™t forget that LookRotationâ€™s forward parameter is a vector that goes directly behind your text, away from camera.
so again if it was on the XY plane, this would be Vector3.forward.
if it was on the XZ plane, this would be Vector3.down and so on.
up, on the other hand is the global up, obviously you can experiment with it.

``````text.rotation = Quaternion.LookRotation(Vector3.forward, Vector3.up);
``````

edit:
oops sorry, thatâ€™s not gonna work

Vector3.forward is the original state, you need the new forward, and well you can use the same rotation we got previously, but apply it to this vector instead

``````var rotX = Quaternion.Euler(someAngle, 0f, 0f);
var rotSpace = Quaternion.FromToRotation(Vector3.right, (p2 - p1).normalized);
text.rotation = rotX * Quaternion.LookRotation(rotSpace * Vector3.forward, Vector3.up);
``````

this will give you full control + the dynamic reorientation so the text always manages to know where the up is. experiment with the actual cardinal vectors, because Iâ€™m doing this from my head, itâ€™s hard to visualize correctly.

I did like this.

``````textObj.transform.rotation = Quaternion.FromToRotation(Vector3.right, dir) * Quaternion.Euler(90, 0f, 0f);
``````

This works fine if my line rendered horizontally. But when my line rendered vertically(like on a wall) texts are not perfectly rotated. How can I identify if my line is rendered horizontally or vertically? Please check the attached images. If line renders horizontally, my text should be upwards and if the line is vertically rendered, I want to show the text forward(looking at the user). I have uploaded how my prefab shows in Unity scene.