Need A Little Math Help

I’ve been at this for several days and I’m having brain cramp.

I’m trying to determine the coordinates of a transform’s corners.

Say a square or rectangle - I’m only concerned with x and z positions.

So given a square transform with a transform.eulerAngles.y rotation, what would be the proper math to determine the positions of the four corners.

Or is there a Unity function or combo of functions that can determine this?

I’ve been trying to use transform.collider.bounds.extents and Mathf.Sin(transform.eulerAngles.y*Mathf.Deg2Rad)) as well as cosine functions, transform.localScale and all sorts of ideas but I’m going nuts trying to get this. I have several trig books in front of me, but I’m having a brain melt with the math.

Thank you for any help with this.

You can create a matrix with Matrix4x4.TRS then trasform your vector3 with Matrix4x4.MultiplyPoint3x4

If you’re using the builtin Unity mesh for a cube, the four (2D) corners would be at:

cube.transform.TransformPoint(.5,0,.5);
cube.transform.TransformPoint(.5,0,-.5);
cube.transform.TransformPoint(-.5,0,-.5);
cube.transform.TransformPoint(-.5,0,.5);

If you’re using another mesh you can use different numbers instead of 0.5. No math necessary. :wink:

Oh, good gosh, that was too easy. Thank you, bro - you unfroze me brain!

	var size = transform.collider.size;
	x = size.x*.5;
	z = size.z*.5;
	corner1.position = transform.TransformPoint(x/size.x,0,z/size.z);
	corner2.position = transform.TransformPoint(x/size.x,0,-z/size.z);
	corner3.position = transform.TransformPoint(-x/size.x,0,-z/size.z);
	corner4.position = transform.TransformPoint(-x/size.x,0,z/size.z);

Okay, so now the size of the cube is determined by getting the collider size. Works like a charm.

If I could just have the last 3 days of my life back!

Thanks again!

also don’t forget you can use bounds instead of collider if you object doesn’t have a collider, or the collider is smaller then the mesh for what ever reason.

Granted a bit more complex… but worth knowing about.

Cheers.