So I’ve obtained my normals form my mesh as in this example
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] normals = mesh.normals;
I then simply want to use these normals in a Quaternion operation like so
orientation = Quaternion.FromToRotation(Vector3.up, normals);
But I get the error
How to convert my Vector3[ ] to Vector3 which is what the Quaternion operation is expecting?
Quaternion.FromToRotation expects a Vector3 as the second parameter. You’re passing “normals” which is an array ov Vector3’s, a collection usually of multiple of the same type. FromToRotation doesn’t know what to do with that, because it has no idea which Vector3 from that array you want to use.
If you just want to see the code work, you could try just picking one of them from the array like this:
if (normals.Length > 0) //check that there actually is at least 1 Vector3 in the array
{
orientation = Quaternion.FromToRotation(Vector3.up, normals[0]); //use the first Vector3 in the array (the one at index 0)
}
But I get the feeling that you don’t really understand something here. Not that I do either But I’d think you would know which one of these Vector3’s you mean to use before getting this far. Maybe if you explain more of what you’re trying to do, someone could better help you further.
Thanks. Trying to orient a placed cube so that it is perpendicular to a scanned mesh of a wall.
I tried simply using the z normal ie normals[2] and then used it like so
orientation = Quaternion.FromToRotation(Vector3.up, normals[2]);
cube.transform.rotation=orientation;
but its not giving me the intended result.
For one thing my cube is being rotated on more than 1 axis.
Assuming the UP = y axis I really only need to rotate my cube on the yaxis.
Quaternions make my head hurt.
I’m wondering if there is any way I can use the transform.Rotate(x,y,z) method instead?
Sorry I don’t know the answer to your question. Hopefully with your explanation, someone else will be able to provide suggestions.
How are you determining where to place the object on the wall? It’s very unlikely that you want to be directly reading the mesh data of your wall to determine the appropriate normal for orienting you placed cube. Usually for this kind of thing you would do a raycast at the wall and use the normal from the RaycastHit to determine how to orient your cube.
Thanks. To be clear I am not placing the cube ‘on the wall’.
In this case it is simply instantiated at the center of the scanned environment or room in this case so that it is equidistant from all 4 walls (or 6 if you include ceiling and floor).
cube= Instantiate(cubePrefab, ARSessionOrigin.transform, false);
Vector3 offset = new Vector3(_midX, 0, _midZ);
cube.transform.Translate(offset, Space.Self);
ok so needs more testing but the following may be working for me!
Vector3 zNormal = normals[2];
cube.transform.rotation = Quaternion.FromToRotation(Vector3.forward, zNormal);