- Home /
Move cube vertices up 2 units (world) whatever cube orientation
Here's my question
I have a cube
I rotate the cube on its x, y and z axis every frame
Finally I'd like to get the image of this cube but 2 units up the original cube. When I say image I mean that the vertices of the cube are translated to their new coordinates. (see picture)
How should I go about doing that... Any ideas ?
Edit Title: changed title from "3d trigonometry question" to "Move cube vertices up 2 units (world) whatever cube orientation". Far more specific ;)
Answer by HarshadK · Dec 01, 2014 at 10:58 AM
If you want to move the cube upwards in the world space then you can translate it by using Vector3.up. Something like:
transform.position += transform.position + (Vector3.up * 2);
This will shift the cube upwards in the world 'y' axis by 2 units, keeping its rotation.
Yep, except that I don't want the cube to be translated but only its vertices..
How can you translate the vertices of a mesh but keep the mesh there itself?
Do you mean you just want to get the new position values for the cube vertices?
In that case you can translate the cube to new position as specified in the answer. Then get the position of its vertices using the $$anonymous$$esh class and then reset the position of the cube back down by 2 units. Something like:
Psuedocode:
// $$anonymous$$ove up by 2 units
transform.position += transform.position + (Vector3.up * 2);
//*** Get the position of vertices of the mesh using $$anonymous$$esh class
// Reset the position of the cube
transform.position += transform.position - (Vector3.up * 2);
Yes, I want to know the new position values for the cube vertices, but in local space. The transform of the mesh should keep its position and orientation...
You can use the above method which will provide you with the location of the vertices relative to the center of the object. You can then use transform.localToWorld$$anonymous$$atrix to convert it to the world positions.
But if I do as you say I will get that :
because the vector3.up will translate the transform on its local axis and the cube is already rotated by 45°...
Answer by molosev · Dec 03, 2014 at 04:38 PM
I finally found a solution without cube translation:
using UnityEngine;
using System.Collections;
public class MoveVerticesUp : MonoBehaviour {
Mesh mesh = null;
Vector3[] localVerticesArray = null;
Vector3[] globalVerticesArray = null;
void Start () {
mesh = GetComponent<MeshFilter>().mesh;
localVerticesArray = mesh.vertices;
globalVerticesArray = mesh.vertices;
} // end Start
void Update () {
for(int i = 0; i < localVerticesArray.Length; i++)
{
globalVerticesArray[i] = transform.TransformPoint(localVerticesArray[i]);
globalVerticesArray[i].y = globalVerticesArray[i].y + 2;
globalVerticesArray[i] = transform.InverseTransformPoint(globalVerticesArray[i]);
}
mesh.vertices = globalVerticesArray;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
} // end Update
} // end class
Works like a charm !
Thanks HarshadK for giving me tools and general flow!
Your answer
Follow this Question
Related Questions
vertices to world position 1 Answer
Specific use of TransformDirection 0 Answers
vertex shader moving vertex up in world space 1 Answer
WorldSpace movement? 1 Answer