简体   繁体   English

Unity-翻译GameObject 3d Cube Edge

[英]Unity - Translate a GameObject 3d Cube Edge

I use a C# script to populate my scene with a couple of cubes, then pick a certain number of them and transform them. 我使用C#脚本用几个多维数据集填充场景,然后选择一定数量的多维数据集并对其进行转换。 The transformation I want to do is as per this image 我想做的变换是根据这张图片

How do I move an edge of a basic Unity 3d Object Cube (in C# script)? 如何移动基本Unity 3d对象立方体的边缘(在C#脚本中)?

You can do this by editing the vertices of the Mesh. 您可以通过编辑网格的顶点来实现。 For example, attach this script to a cube and adjust the camera so you can see things shift around as you hit the space bar. 例如,将此脚本附加到多维数据集并调整相机,以便您在按空格键时可以看到事物在四处移动。 You should see the box shift and get an idea how to get what you want. 您应该会看到方框的变化,并了解如何获得所需的东西。

public class CubeScript : MonoBehaviour {

int vert_num = 0;
Mesh mesh;
Vector3[] verts;

// Use this for initialization
void Start () {
    mesh = GetComponent<MeshFilter>().mesh;
    verts = mesh.vertices;
}

// Update is called once per frame
void Update ()
{
    if (Input.GetKeyDown (KeyCode.Space)) {

        // Loop back around after the last vert
        if (vert_num >= verts.Length) {
            vert_num = 0;
        }

        // Move the next vert and echo its number
        Debug.Log("Moving vert#: " + vert_num);
        verts[vert_num] += Vector3.up * 0.1f;
        mesh.vertices = verts;

        vert_num += 1;
    }
}

} }

For more info see: https://docs.unity3d.com/ScriptReference/Mesh.html 有关更多信息,请参见: https : //docs.unity3d.com/ScriptReference/Mesh.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM