- Home /
Edge of a cube
Given a cube of scale 2,1,1, and without knowing its rotation, i want to find where its right-most point is.
So the distance from the center to its right will be further if its flat, compared to if its standing up.
Answer by aldonaletto · Nov 26, 2012 at 01:09 AM
You could get the mesh vertices, convert to world coordinates and then to the camera local space to find the one with greatest X coordinate - like this:
function FindRightmostPoint(obj: GameObject): Vector3 {
var mesh: Mesh = GetComponent(MeshFilter).mesh;
var verts: Vector3[] = mesh.vertices; // get cube vertices
var xMax: float = Mathf.NegativeInfinity;
var pMax: Vector3;
for (var vert: Vector3 in verts){
// get vertex in world coordinates:
var worldP = obj.transform.TransformPoint(vert);
// convert to camera's local coordinates:
var localP = transform.InverseTransformPoint(worldP);
if (localP.x > xMax){
xMax = localP.x;
pMax = worldP;
}
}
// return world coordinates of rightmost vertex:
return pMax;
}
This function should be placed in a camera script: it finds the rightmost vertex of the object obj from the camera's point of view, and return it in world coordinates.
var localP = transform.InverseTransformPoint(p);
What is p? You dont have another reference to it. Is it worldP?
$$anonymous$$y bad: it should be worldP - answer fixed now.
I'm yoonkuu... Tryin to find a way to contact you. Can you make this for me?? What's your email?? comment below so that i can check your answer? Thanks I have ragespline and all the other stuff, but don't know how to make this kinda object.. Can u tell me your email so that i can send you What i worked on? and you can look up here for the code.. http://forum.unity3d.com/threads/54092-Draw-Polygon Can u implement it for me/?? I really need to make that cube.. Here's a much more depth look at it http://www.youtube.com/watch?v=g7zTm$$anonymous$$vvUpk At some mark, it shows the ragecube thing.
Answer by Bunny83 · Nov 25, 2012 at 11:34 PM
What "right" are you talking about? The positive x axis in worldspace? If so you can simply use:
otherwise be more specific.
I should have said, what appears to be right relative to the camera.
That's something totally different :)
Now it depends on if you use a perspective camera (this is very hard) or an orthographic camera (not that hard).