亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 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 關注
  • 267 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號