- Home /
Finding cube corners
I have an object in front of my character which is a cube - it moves with the player and rotates on y axis when 'q' and 'e' buttons are pushed. I need to find x and z values of this cube's corners (doesn't matter if it's the bottom set or one on the top.)
I tried making Vector3 type variables and setting their values to (cube.transform.position.x - cube.transform.localscale.x / 2) same for z (pluses and minuses change depending on the corner), but that didn't work when the cube was being rotated - I used gizmos to check them. Is there any easy way to solve this?
Answer by sneftel · Jul 18, 2011 at 04:40 PM
cube.transform.localScale / 2
will indeed get you the local position of one of the cube's corners; Vector3.Scale(cube.transform.localScale / 2, new Vector3(1,1,-1))
will get you one of the others (different combinations of 1 and -1 there will get you all eight). Then, to find them in world space, use cube.transform.TransformPoint()
.
Sorry, that code snippet isn't quite correct. Pass new Vector3(1,1,-1)
or whichever directly to TransformPoint()
, rather than running it through Scale
.
Answer by 4illeen · Jul 18, 2011 at 05:35 PM
It worked thanks - but I've got another problem. When I set cube's scale in Start() cube.transform.localScale = new Vector3(2, 1, 1); something weird happens. Here's the pic
, seems like whenever the scale value isn't equal to 1 it adds to the position of the point somehow.
Answer by jordanmisiura · Oct 31, 2011 at 06:06 AM
instead of local scale, why not get the mesh filter, then get it's bounds?
MeshFilter mshFilt = ((MeshFilter)this.GetComponent()); Mesh cubeMesh = mshFilt.mesh;
someTransform.transform.position = this.transform.TransformPoint(cubeMesh.bounds.min);
using bounds.min and bounds.max should get you two flavors of the cube's corners, and taking various values from the min and max's x,y,z coords should get you the other six.
Answer by jordanmisiura · Oct 31, 2011 at 06:06 AM
MeshFilter mshFilt = ((MeshFilter)this.GetComponent()); Mesh gridMesh = mshFilt.mesh;
yellowCube1.transform.position = this.transform.TransformPoint(gridMesh.bounds.min);
that should get you one of the lower corners. Using bounds.max should get you one of the upper corners on the other side probably.
Using various flavors of the max and min's x,y,z coords should get you all 8 corners.
Your answer
![](https://koobas.hobune.stream/wayback/20220612150217im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# sticking to Cube corners position (raycasting AI script) 1 Answer
How would I go about making a cube "tumble" off of corners and such? 2 Answers
Finding the dimensions of a cube, exploiting them. 2 Answers
Recover project from a different computer? 1 Answer
How do i find a bone on my character within a script? 2 Answers