- Home /
Generating a cube mesh with two given points
I'm attempting to create a script that takes two positions from the player, and creates a cube between those two points with a given height and depth. I can manage to generate a single face (Unity's Mesh library makes it easy to do), but I run into trouble when I try to create the other faces. Since the mesh is generated between two points, it has the appearance of being "rotated," but its forward vector remains relative to the world since the transform is never actually rotated. You can see an example of what I mean here:
This makes generating the other faces less than trivial (as far as I can tell) since I can't just use the first face's transform.forward to place the remaining vertices. If anyone has any experience with this let me know, and if you need any elaboration/code feel free to ask. Thanks!
Answer by Waz · Aug 14, 2011 at 02:43 PM
The Cross Product of two vectors is a vector pointing at right angles to both those vectors. Think about how that gives you exactly the vector you need: one pointing in the depth direction of your cube, the displacement to the 4 vertices of the back face of the cube, given each of the 4 front vertices.
(BTW, well done getting that far first - you've actually done the hard part!)
Brilliant! I'm less than familiar with vector math and imagined there would be a simple way to do it. Thanks so much!
Your answer
Follow this Question
Related Questions
Rotating a created mesh around it's pivot 2 Answers
Custom Mesh UV Problem 1 Answer
Get face normal and get its local Euler Angles? 3 Answers
Moving and rolling a cube 1 Answer
Rotating direction vectors 1 Answer