Jump to content
Minamu

Finding the bottom vertices on a cube?

Recommended Posts

Hey, first time posting here, not sure if I'm in the right place though.

I've considered asking this in the Unity Answers forum but I thought I might check here first since it's directly related to ProBuilder, and C# programming.

Basically, I have a regular 1x2x1 PB cube, Unity scale 1x1x1, as a character in my game. I want to add raycasts to each of the bottom corners of this cube, for jumping mechanics purposes. The problem is finding these vertices, since the origo is now different compared to the standard Unity cube. Using transform.position.x is the same as always, but finding the other three corners is where I'm stuck, and I can't get the raycasts to actually stick to the object's corners as it rotates.

Any ideas on how to go about this issue? :)

Share this post


Link to post
Share on other sites

Instead of going by the vertices you could just take the bottom four corners of the bounds.

 

Vector3 center = bounds.center;
Vector3 extents = bounds.size * .5f;

Vector3 a = new Vector3(center.x - extents.x, center.y - extents.y, center.z - extents.z);
Vector3 b = new Vector3(center.x - extents.x, center.y - extents.y, center.z + extents.z);
Vector3 c = new Vector3(center.x + extents.x, center.y - extents.y, center.z - extents.z);
Vector3 d = new Vector3(center.x + extents.x, center.y - extents.y, center.z + extents.z);

 

Share this post


Link to post
Share on other sites

Thanks!

Would this approach be rotation agnostic then? Meaning, it'll find the right corner values regardless of the direction the player is looking in? The game uses a regular third person camera and as you turn the camera, the player's body turns the same number of degrees and that is currently breaking our positioning of the rays.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×