转变位置。

发布于 2025-02-12 16:03:02 字数 1724 浏览 2 评论 0原文

我在C#编程方面有些新。

我正在制作一台老虎机,而我面临的问题归结为transform.position.y。我有每个卷轴的3个水平图像条。每个符号在Photoshop中恰好是200px,当我手动将一个值2(见下文)键入检查器时,符号按预期对准。这些符号像我在启动上随机对其进行随机化的方法一样绘制出来,因此每次玩家启动游戏时,机器都有不同的符号,并且效果很好 - 它显示了每个符号,每个符号都应该在哪里,完美无瑕时间。符号如下:

//Orange
transform.position = new Vector2(transform.position.x, 0f);
transform.position = new Vector2(transform.position.x, transform.position.y - 22);

//Pear
transform.position = new Vector2(transform.position.x, 0f);
transform.position = new Vector2(transform.position.x, transform.position.y - 20);

//Plumb
transform.position = new Vector2(transform.position.x, 0f);
transform.position = new Vector2(transform.position.x, transform.position.y - 18);

等等:等等。

当旋转的时间到来时,执行以下循环以每次将卷轴向下转动2,以将每个符号排成应该的位置,首先它可以工作,但是有时符号并不完全位置而且它们是由几个单元向上或向下的,这很明显。

for (int i = 0; i < 50; i++){
            //Do other things that do not relate to my problem
      
            //If the spinning reel moves out of bounds, snap it back to position - works
            if (transform.position.y <= -22)
                transform.position = new Vector2(transform.position.x, +22);
            //Does not work as intended
            //Transform.pos.x is not 0 so it stays within reel bounds, it works.
            transform.position = new Vector2(transform.position.x, transform.position.y - 2);

            //Do other things that do not relate to my problem

        }

我已经尝试了一些不同的事情来解决这个问题,而且它们都没有起作用,在几个旋转一个或两个符号不会像应有的那样排队之后,总是会发生同样的事情。我尝试使用Mathf.floorMathf.ceil(结果非常糟糕)和Mathf.Round - 我也已经施放了全部这些方法toint,因为我正在使用一个数字。

我能做些什么?我想我正在做正确的事,但我可能错了。

谢谢!

I'm somewhat new at C# programming.

I'm making a slot machine and the issue I am facing comes down to transform.position.y. I have 3 horizontal image strips for each reel. Each symbol is exactly 200px apart in Photoshop and when I manually type in a value of 2 (see below) into the inspector the symbols line up as expected. The symbols are mapped out like so in a method I use to randomize them on startup so the machine has different symbols every time the player starts a game, and that works fine - it shows each symbol where it is supposed to be, flawlessly and every time. The symbols are mapped as follows:

//Orange
transform.position = new Vector2(transform.position.x, 0f);
transform.position = new Vector2(transform.position.x, transform.position.y - 22);

//Pear
transform.position = new Vector2(transform.position.x, 0f);
transform.position = new Vector2(transform.position.x, transform.position.y - 20);

//Plumb
transform.position = new Vector2(transform.position.x, 0f);
transform.position = new Vector2(transform.position.x, transform.position.y - 18);

and etc down the reel.

When it comes time for the spin, the following loop is executed in order to transform the reels downwards by 2 each time to line up each symbol where it's supposed to be, and at first it works, however sometimes the symbols are not exactly in place and they are either up or down by a few units and it's noticeable.

for (int i = 0; i < 50; i++){
            //Do other things that do not relate to my problem
      
            //If the spinning reel moves out of bounds, snap it back to position - works
            if (transform.position.y <= -22)
                transform.position = new Vector2(transform.position.x, +22);
            //Does not work as intended
            //Transform.pos.x is not 0 so it stays within reel bounds, it works.
            transform.position = new Vector2(transform.position.x, transform.position.y - 2);

            //Do other things that do not relate to my problem

        }

I have tried a few different things to fix this, and none of them work, the same thing always happens, after a few spins one or two symbols will not line up as it should. I have tried using MathF.Floor, MathF.Ceil (which had very bad results) and MathF.Round - and I have also cast all of these methods ToInt because I'm working with a single number.

What can I do? I think I'm doing the right thing, but I could be wrong.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

被你宠の有点坏 2025-02-19 16:03:02

我不确定它是否会有所帮助,但是您可能需要使用time.deltatime https://docs.unity3d.com/scriptreference/time-deltatime.html ,以确保某些事情在框架上发射吗?

// Note: untested as it is difficult to spin up new unity projects
for (int i = 0; i < 50; i++)
{
        if (transform.position.y <= -22)
        {
             transform.position = new Vector2(transform.position.x, +22 * Time.deltaTime);
        }
        
        transform.position = new Vector2(transform.position.x, 
            transform.position.y - 2 * Time.deltaTime);

}

另外,您可能会在这里找到更好的指导: https://gamedev.stackexchange.com/

I am not sure it will help, but you may need to use the Time.DeltaTime https://docs.unity3d.com/ScriptReference/Time-deltaTime.html, to make certain things are firing on frame?

// Note: untested as it is difficult to spin up new unity projects
for (int i = 0; i < 50; i++)
{
        if (transform.position.y <= -22)
        {
             transform.position = new Vector2(transform.position.x, +22 * Time.deltaTime);
        }
        
        transform.position = new Vector2(transform.position.x, 
            transform.position.y - 2 * Time.deltaTime);

}

Also you might find better guidance here: https://gamedev.stackexchange.com/

思慕 2025-02-19 16:03:02

事实证明,在我的情况下,我一直使用的精灵没有正确地对齐,这很尴尬,但这就是我们学习的方式,对吗?在这里和那里更改精灵会导致预期结果,这是由于如何布置而令人困惑的。哦,我想这些事情发生了。如果任何人将来会遇到类似的东西,我将留下来。

在这种情况下,我对其他任何人的建议是打开您的单独精灵,并叠加在彼此的顶部。明显的错误将立即弹出。无需数学或舍入。

It turns out in my case that the sprites I have been using were not aligned properly, which is very embarrassing but that's how we learn, right? Altering the sprites just a little here and there leads to expected results, which is baffling due to how they are laid out. Oh well, these things happen I guess. I'll leave this up in case anyone runs into something similar in future.

My advice to anyone else in this sort of situation is to open your seperate sprites and overlay them on top of eachother. Glaring mistakes will pop out immediately. No math or rounding needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文