- Home /
How to find the closest 4 verts/corners ( along with their orientation ) of a rectangular object in relationship with another object?
Hi,
In my game, the player object picks up and drops rectangular objects ( rects ). Before the player is allowed to pick up one of these rects, the rects need to have one face parallel with the x/z. If one face of the rect is parallel with the x/z then the player can pick up the object, but as the player moves into position, it needs to rotate on the y axes and extend or retract it's arms so the player object's corners align with the closest 4 corners/verts of the rect ( highest verts on the y axes ).
For finding out if the rects are sitting flat with one face parallel with the x/z axes, renderer.bounds.size.y seems to give good results because the rects are 3x3x6, so it will give me 3 or 6 if it is flat. Please let me know if that is bad practice. For finding out the closest 4 corners/verts and the y rotation that the player object would have to assume to line up with the 4 verts, I was thinking of using mesh.vertices to find the certs with the highest y value and calculating the angle between them to attain the y rotation needed for alignment, but is there a more standard way to do something like this without making extra child game objects for each corner of the rects??
oops, mesh.vertexCount is 24, I guess I'll have to find the closest 12 verts. Open to ideas, as I'm sure there are better approaches.
Are the shapes always perfect rectangles, if so you could check that the x and y rotations are multiples of 90? I'm not sure I've completely grasped the situation of your problem.
Answer by Scribe · Feb 06, 2014 at 12:37 AM
Hi,
I think I've found quite a good way of doing this using normals:
var mesh : Mesh;
var normals : Vector3[];
function Start(){
mesh = GetComponent(MeshFilter).mesh;
normals = mesh.normals;
}
function Update () {
for (var i = 0; i < normals.Length; i++){
var V = transform.TransformDirection(normals[i]).normalized;
if(Mathf.Abs(V.y) == 1){
// if it gets here then there is a face that faces along the y-axis
break;
}
}
}
Currently this is set up to work if it is attached to your 'rects' so it may need to be changed for your situation, but it should be reliable and should be more efficient than searching through vertices (as all 3D objects have more vertices than faces).
Scribe
It's strange, I originally found that the rotation was tough to find, but it seems that the y rotation of the rects is giving consistent results regardless of the rotation on the other axes. When I first tested it in the editor during runtime, I was rotating y and z and it was just showing the y axes rotation. I guess I have to study euler angles more. As long as one face is pointing along the y axis, the euler y rotation is giving me what I need.
Thanks for what you have done above, although do you think that it would be more efficient than renderer.bounds.size.y??
Glad you got it working, honestly I have no idea about this options efficiency in comparison to renderer.bounds though you can test it quite easily by instantiating a few thousand cubes with each variation of the script attached.
Your answer
Follow this Question
Related Questions
Generate mesh from raycast positions, independent of rotations 0 Answers
Changing vertex positions of a uGUI Text Mesh 0 Answers
Anchor TextMesh position to objects. 0 Answers
UnityEngine.UI.Text characters mesh 0 Answers
Editing vertices 1 Answer