为了账号安全,请及时绑定邮箱和手机立即绑定

在 Unity 中生成“斜坡”对象的算法

在 Unity 中生成“斜坡”对象的算法

C#
慕容3067478 2021-06-30 15:03:05
我正在 Unity 中为我的 A 级计算机科学项目创建一个基本模拟器。目前,用户可以通过选择相关工具并单击并拖动来确定框的两个相对角,从而确定其尺寸来绘制框(板条箱)对象。该盒子由一个单独的预制件组成,该预制件被实例化并相应地改变其大小。它的代码如下:void Start () {    boxAnim = boxButton.GetComponent<Animator>();}// Update is called once per framevoid Update(){       //sets the mouseDown and mouseHeld bools and the mouse position Vector3    mouseDown = Input.GetMouseButtonDown(0);    mouseHeld = Input.GetMouseButton(0);    mousePosition = Input.mousePosition;    //checks if the user has started to draw    if (mouseDown && !draw)    {        draw = true;        originalMousePosition = mousePosition;    }    //checking if the user has released the mouse    if (draw && !mouseHeld)    {        finalMousePosition = mousePosition;        draw = false;        if (boxAnim.GetBool("Pressed") == true) //if the box draw button is pressed        {            boxDraw(originalMousePosition, finalMousePosition); //draws crate        }    }}以类似的方式,我希望能够创建一个“斜坡”对象,以便用户可以单击并拖动以确定基本宽度,然后再次单击以确定仰角/高度,(斜坡将始终是一个直角三角形。)问题在于我希望将斜坡作为我创建的精灵,而不仅仅是基本的块颜色。然而,单个精灵只有一个仰角,并且没有变换能够改变这一点(据我所知。)显然我不想为每个角度创建不同的精灵,所以有什么我可以做的吗?我想的解决方案是,是否有某种功能可以改变代码中矢量图像的节点,但我很确定这不存在。
查看完整描述

2 回答

?
慕丝7291255

TA贡献1859条经验 获得超6个赞

我有一个使用网格和多边形碰撞器来解决部分问题的方法。我现在有一个函数可以创建一个具有给定宽度和高度的直角三角形以及一个三角形形状的碰撞器:


using UnityEngine;

using System.Collections;


public class createMesh : MonoBehaviour {


    public float width = 5f;

    public float height = 5f;


    public PolygonCollider2D polyCollider;


    void Start()

    {

        polyCollider = GetComponent<PolygonCollider2D>();

    }


    // Update is called once per frame

    void Update () {

        TriangleMesh(width, height);

    }


    void TriangleMesh(float width, float height)

    {

        MeshFilter mf = GetComponent<MeshFilter>();

        Mesh mesh = new Mesh();

        mf.mesh = mesh;


        //Verticies

        Vector3[] verticies = new Vector3[3]

        {

            new Vector3(0,0,0), new Vector3(width, 0, 0), new Vector3(0, 

height, 0)

        };


        //Triangles

        int[] tri = new int[3];


        tri[0] = 0;

        tri[1] = 2;

        tri[2] = 1;


        //normals

        Vector3[] normals = new Vector3[3];


        normals[0] = -Vector3.forward;

        normals[1] = -Vector3.forward;

        normals[2] = -Vector3.forward;


        //UVs

        Vector2[] uv = new Vector2[3];


        uv[0] = new Vector2(0, 0);

        uv[0] = new Vector2(1, 0);

        uv[0] = new Vector2(0, 1);


        //initialise

        mesh.vertices = verticies;

        mesh.triangles = tri;

        mesh.normals = normals;

        mesh.uv = uv;


        //setting up collider

        polyCollider.pathCount = 1;


        Vector2[] path = new Vector2[3]

        {

            new Vector2(0,0), new Vector2(0, height), new Vector2(width, 0)

        };


        polyCollider.SetPath(0, path);


    }

}

我只需要将此函数放入与我绘制框的代码非常相似的代码中,以便用户可以指定宽度和高度。


查看完整回答
反对 回复 2021-07-10
  • 2 回答
  • 0 关注
  • 237 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信