微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Unity C#

如何解决如何在 Unity C#

对于 Unity 和 C# 来说还是很新的。在设置/更改对象的位置时,我通常可以找到自己的方法,但是当涉及到旋转时,它仍然让我感到难以置信,包括四元数、欧拉角等。

我试图检查一个物体(一个头)是否面向某个方向,如果它没有面向那个方向,它需要根据它的前进方向(向上、向下、向左或向右)将其设置为那个方向.这是针对我目前正在开发的蛇游戏(如旧的自上而下的诺基亚游戏)。

我试着查了一下,但没有一个答案是针对我正在尝试做的事情。

截至目前,这是我用于玩家输入的内容(一段代码):

private int GetAxisRaw(Axis axis)
    {
        if (axis == Axis.Horizontal)
        {
            bool left = Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A);
            bool right = Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D);

            if (left)
            {
                snakeHeadRotation.transform.Rotate(0,180,0); // Keeps rotating when left is pressed
                return -1;
            }
            if (right)
            {
                snakeHeadRotation.transform.Rotate(0,-180,0);
                return 1;
            }

            return 0;
        }

如你所见,snakeHeadRotation.transform.Rotate(0,0);例如,每次玩家按下左键时都会被调用,从而即使蛇仍在向左移动时也会转动蛇的头部。

我的逻辑是将旋转值存储在某处(可能键入 Quaternion/bool)并检查该旋转值是否与设置为左的当前旋转值匹配(即 snakeHeadRotation.transform.Rotate(0,0);) 如果是这种情况,请不要做任何事情。如果不是,请将其设置为 snakeHeadRotation.transform.Rotate(0,0);例如。

代码看起来像(写出来):

private int GetAxisRaw(Axis axis)
{
    if (axis == Axis.Horizontal)
    {
        bool left = Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A);
        bool right = Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D);

        if (left && rotation is not equal to snakeHeadRotation.transform.Rotate(0,0))
        {
            snakeHeadRotation.transform.Rotate(0,0); 
            return -1;
        }
        if (right)
        {
            snakeHeadRotation.transform.Rotate(0,0);
            return 1;
        }

        return 0;
    }

如前所述,我非常不熟悉设置或存储旋转值。我怎样才能做到这一点?这甚至是正确的方法还是有更合乎逻辑的方法?这个我自己也想不通..

我希望我的解释是正确的,我通常不擅长解释事情。

解决方法

我只想为每个方向创建 4 个四元数,并根据按下的键设置旋转(例如 hey.rotation = leftQ)。这是最简单的方法。

我目前不在我的电脑旁,但我会尽可能寻找其他解决方案

,

使用 Transform.Rotate(x) 会使您的头部相对于您当前的头部方向旋转 x 度。

我想您想要的是根据您按下的箭头键设置头部的绝对旋转。因此,即使您多次按下同一个箭头键,头部也会保持在正确的方向。

为此,您可以为每个头部方向创建 4 次旋转,并根据需要使用它们。 例如,如果您的头部默认朝向 forward

// create your directions
private static Quaternion forwardDirection = Quaternion.Identity;
private static Quaternion rightDirection = Quaternion.FromToRotation(Vector3.forward,Vector3.right);
private static Quaternion leftDirection = Quaternion.FromToRotation(Vector3.forward,Vector3.left);
private static Quaternion backDirection = Quaternion.FromToRotation(Vector3.forward,Vector3.back);

...

private int GetAxisRaw(Axis axis)
{
    ...

    if (left)
    {
        // set the absolute direction of your head to the left
        snakeHeadRotation.transform.rotation = leftDirection; 
        return -1;
    }
    if (right)
    {
        // set the absolute direction of your head to the right
        snakeHeadRotation.transform.rotation = rightDirection;
        return 1;
    }

    ...
}
,

您可以将当前旋转与 4 个方向的已知旋转进行比较。 但不要那样做,这是不好的做法,原因如下:

  • 如果以后你想添加动画怎么办,例如头部在 0.5 秒内转动,而不是立即转动?
  • 仅比较 4 个浮点数以检查 4 个可能的方向之一是低效的。

改为创建一个枚举:

public enum Direction
{
    Up,Down,Left,Right
}

将这种类型的成员添加到您的行为中,以跟踪您的蛇头的去向。 类似的东西:

    Direction direction;
...
private int GetAxisRaw(Axis axis)
{
    switch (direction)
    {
        case Direction.Up:
            if (left && !right)
               direction = Direction.Left;
               ... turn snake left ...
            else if (right && !left)
               direction = Direction.Right;
               ... turn snake right ...
            // ignore inputs for up or down if going up
        break;
        ... repeat for the other directions and possible inputs ...
    }
    ...
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。