简体   繁体   中英

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. 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)?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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