# Get simple rectangle shaped mesh face direction

i have a tube that contains several separated rectangle shaped mesh objects. My goal is to spawn an object in the center of each rectangle and face perpendicular to the rectangle surface

On each separated rectangle i have this code:

``````Mesh mesh = GetComponent<MeshFilter>().mesh;
int[] triangles = mesh.triangles;

Vector3 n0=Vector2.zero,n1=Vector2.zero,n2=Vector2.zero;
for (int i = 0; i < mesh.triangles.Length; i += 3)
{
n0 = mesh.vertices[mesh.triangles[i + 0]];
n1 = mesh.vertices[mesh.triangles[i + 1]];
n2 = mesh.vertices[mesh.triangles[i + 2]];
}

Vector3 a = Vector3.Cross (n1 - n2, n2 - n0).normalized;

Instantiate(prefab, transform.renderer.bounds.center,Quaternion.LookRotation(a));
``````

Result is this:

My goal is this

any ideas ??

Can this works? (Eventually try changing the 180 angle)

``````Instantiate (prefab, transform.renderer.bounds.center, transform.rotation * Quaternion.Euler (180,0,0 ));
``````

I suspect the transform is that of the tube and you have no transform for the rects. Right? If so I am wrongâ€¦ sorry.

If you have no transform for the rectangles, try this:

``````Instantiate (prefab, transform.renderer.bounds.center, Quaternion.LookRotation (transform.position, transform.up) * Quaternion.Euler (180, 0, 0 ));
``````

You can omit the Euler to invert the rotation and you can try changing the transform.up to transform.forward or right (depending on the tube orientation).

This is my last betâ€¦

``````Instantiate (prefab, transform.renderer.bounds.center, Quaternion.FromToRotation (Vector3.up, a));
``````

You can try .forward or .right depending on the prefab orientation.

Thank you for helping but all of the individual rectangles game objects have the same rotation so i cant use the standard functions like this

I think the solution will be to get the perpendicular to the mesh rectangle front facing side
What do you think?

Individual rectangles game objects have the same rotation

I think they have the same localRotation (the same you are looking in the editor) but their transform.up is that green line you are seeing above.

The first solution I am almost sure could works. I just wonder if you tryed it.

yes i tried the code but the didnt solve it unfortunately . Also the transform.up points to the same direction for all objects (not perpendicular) , i think it cannot be seeing clearly from pics above â€¦ take a look at this new pic with the first code you suggested

Any thoughts ??

just a quick updateâ€¦ i found that (in my original code) if dont use â€śtransform.renderer.bounds.centerâ€ť location in the instantiate function but use â€śmesh.bounds.centerâ€ť the rotation is correct but position is way off

``````Instantiate(prefab, mesh.bounds.center,Quaternion.LookRotation(a));
``````

any thoughts/ideas on that?

If you can send me a sample scene I try to figure outâ€¦ Iâ€™m having an hard time just in understanding your setup

You can try:

• Transform t = Instantiate (â€¦ mesh.bounds.center
• t.position = transform.renderer.bounds.center;

If your asset is facing the z axis when imported, doing â€śobj.transform.foward = direction to faceâ€ť should suffice.
To illustrate the exemple i made you a sample project with a scrolling inifinte tube that you can download here:
https://www.sugarsync.com/pf/D2307301_98582733_600566

for the following result:

3 Likes

Thanks! I have not played yet with procedural meshes, so I think I can learn something from thatâ€¦
Yutubeâ€¦ nice name!

1 Like

Mesh vertices use local positions, need to convert it to world position to calculate center rectangle.

I used a Quad as rectangle in this example. Spawned object must look at to forward axis.

``````using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter))]
public class temp : MonoBehaviour {

public GameObject prefab;

GameObject instancePrefab;

void Start(){

instancePrefab = (GameObject)Instantiate(prefab);

}

void prefabLookAt(){

Matrix4x4 localToWorld = transform.localToWorldMatrix;

Vector3 normal = Vector3.Cross(v1-v0,v2-v0);

Vector3 center = Vector3.zero;

int i;

center /= (float)i;

instancePrefab.transform.position = center;
instancePrefab.transform.rotation = Quaternion.LookRotation(normal);

}

void Update(){

prefabLookAt();

}

}
``````
3 Likes

Sorry for the late reply , i had some free time today and tested isGreen code and it solved my issue
everything works as it should be , thank you for helping me out.

I will also test the Yseron source with procedural mesh as it looks interesting.

thanks again for all the help

1 Like