gpt4 book ai didi

c# - Unity中生成 "ramp"对象的算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:16:08 27 4
gpt4 key购买 nike

我正在 Unity 中为我的 A-level 计算机科学项目创建一个基本模拟器。目前,用户可以通过选择相关工具并单击并拖动以确定盒子的两个相对角来绘制盒子( crate )对象,从而确定其尺寸。

这个盒子由一个预制件组成,它被实例化并相应地改变了它的大小。它的代码如下:

void Start () {
boxAnim = boxButton.GetComponent<Animator>();
}

// Update is called once per frame
void 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
}
}
}

void boxDraw(Vector3 start, Vector3 end)
{
//asigns world coordinates for the start and end mouse positions
worldStart = Camera.main.ScreenToWorldPoint(start);
worldEnd = Camera.main.ScreenToWorldPoint(end);
if (worldStart.y >= -3.2f && worldEnd.y >= -3.2f)
{
//determines the size of box to be drawn
boxSize.x = Mathf.Abs(worldStart.x - worldEnd.x);
boxSize.y = Mathf.Abs(worldStart.y - worldEnd.y);
//crate sprite is 175px wide, 175/50 = 3.5 (50px per unit) so the scale factor must be the size, divided by 3.5
boxScaleFactor.x = boxSize.x / 3.5f;
boxScaleFactor.y = boxSize.y / 3.5f;
//initial scale of the box is 1 (this isn't necessary but makes reading program easier)
boxScale.x = 1 * boxScaleFactor.x;
boxScale.y = 1 * boxScaleFactor.y;
//creates a new crate under the name newBox and alters its size
GameObject newBox = Instantiate(box, normalCoords(start, end), box.transform.rotation) as GameObject;
newBox.transform.localScale = boxScale;
}
}

Vector3 normalCoords(Vector3 start, Vector3 end)
{
//takes start and end coordinates as position coordinates and returns a world coordinate coordinate for the box
if(end.x > start.x)
{
start.x = start.x + (Mathf.Abs(start.x - end.x) / 2f);
}
else
{
start.x = start.x - (Mathf.Abs(start.x - end.x) / 2f);
}
if(end.y > start.y)
{
start.y = start.y + (Mathf.Abs(start.y - end.y) / 2f);
}
else
{
start.y = start.y - (Mathf.Abs(start.y - end.y) / 2f);
}
start = Camera.main.ScreenToWorldPoint(new Vector3(start.x, start.y, 0f));
return start;
}

以类似的方式,我希望能够创建一个“坡道”对象,以便用户可以单击并拖动以确定基本宽度,然后再次单击以确定仰角/高度,(斜坡将始终是一个直角三角形。)问题在于我想将斜坡作为我创建的 Sprite ,而不仅仅是基本的 block 颜色。然而,单个 Sprite 只会有一个单一的仰角,并且没有任何变换能够改变它(据我所知)。显然我不想为每个角度创建不同的 Sprite ,所以有什么我可以做的吗?

我当时考虑的解决方案是,是否有某种功能可以让我在代码中更改矢量图像的节点,但我很确定这不存在。

编辑:为了澄清这是一个 2D 环境,代码包含 Vector3s 只是因为那是我习惯的

最佳答案

你提到 Sprite 是一个 2D 对象(好吧,它实际上非常像一个 Quad,被认为是 3D)但是你在你的问题的其他部分和你的代码中引用了完整的 3D,我认为这让人们感到困惑,因为为 Sprite 创建纹理是一个非常不同的问题。我假设您错误地提到了 Sprite,并且您实际上想要一个 3D 对象(反正 Unity 内部大部分时间都是 3D 的),如果需要,它只能有一侧

尽管您确实需要熟悉 Mesh 类,但您可以毫无问题地从代码创建 3D 形状,并且掌握即时创建三角形需要一些练习

这里有几个很好的起点

https://docs.unity3d.com/Manual/Example-CreatingaBillboardPlane.html https://docs.unity3d.com/ScriptReference/Mesh.html

关于c# - Unity中生成 "ramp"对象的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50580216/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com