c# - Unity 2D Top Down Shooter运动问题C#

标签 c# unity3d game-physics topdown

我正在尝试在 Unity 中制作一个简单的 2D 自上而下射击游戏。我有基本的鼠标移动和跟随,但是移动有问题。

每当您同时按下“向上”和“向右”时,它就会像您期望的那样沿对角线移动,但是当您放开向上键时,它会继续沿对角线移动到我希望它向右移动的位置。我处理运动的代码是:

private void Update () 
{
    if (Input.GetKey(Up)) {
        Debug.Log("UP");
        Vector3 velUp = rigidbody2D.velocity;
        velUp.y = walkSpeed;
        rigidbody2D.velocity = velUp;
    }
    else if (Input.GetKey(Down)) {
        Vector3 velDown = rigidbody2D.velocity;
        velDown.y = walkSpeed*-1;
        rigidbody2D.velocity = velDown;
    }
    else if (Input.GetKey(Left)) {
        Vector3 velLeft = rigidbody2D.velocity;
        velLeft.x = walkSpeed*-1;
        rigidbody2D.velocity = velLeft;

    }
    else if (Input.GetKey(Right)) {
        Vector3 velRight = rigidbody2D.velocity;
        velRight.x = walkSpeed;
        rigidbody2D.velocity = velRight;
    }
    else {
        Vector3 velStop = rigidbody2D.velocity;
        velStop.x = 0;
        velStop.y = 0;
        rigidbody2D.velocity = velStop;
    }

    //rotation
    Vector3 mousePos = Input.mousePosition;

    Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
    mousePos.x = mousePos.x - objectPos.x;
    mousePos.y = mousePos.y - objectPos.y;

    float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}

我怎样才能让机芯像我提到的那样运行?像这样沿对角线移动会使运动看起来不正常。

非常感谢任何帮助。谢谢。

最佳答案

您从速度 0 开始,然后将所有运动方向相加。然后你标准化矢量并将其缩放到你的移动速度。否则玩家移动得更快,如果走对角线。

我还没有检查代码,但是像这样的东西会做:

void Update () {
    Vector3 vel = new Vector3();

    if(Input.GetKey(Up)){
        Debug.Log("UP");
        Vector3 velUp = new Vector3();
        // just use 1 to set the direction.
        velUp.y = 1;
        vel += velUp;
    }
    else if(Input.GetKey(Down)){
        Vector3 velDown = new Vector3();
        velDown.y = -1;
        vel += velDown;
    }

    // no else here. Combinations of up/down and left/right are fine.
    if(Input.GetKey(Left)){
        Vector3 velLeft = new Vector3();
        velLeft.x = -1;
        vel += velLeft;
    }
    else if(Input.GetKey(Right)){
        Vector3 velRight = new Vector3();
        velRight.x = 1;
        vel += velRight;
    }

    // check if player wants to move at all. Don't check exactly for 0 to avoid rounding errors
    // (magnitude will be 0, 1 or sqrt(2) here)
    if (vel.magnitude > 0.001) {
      Vector3.Normalize(vel);
      vel *= walkSpeed;
      rigidbody2D.velocity = vel;
    }

    //rotation
    Vector3 mousePos = Input.mousePosition;

    Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
    mousePos.x = mousePos.x - objectPos.x;
    mousePos.y = mousePos.y - objectPos.y;

    float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}

关于 Normalize 看看这张图片。

Vector addition

如果您只向上或向右行走,您将以速度 1 移动(稍后我们将其乘以您想要的速度)。但是如果你走对角线,你走的速度大约是所需速度的 1.4 倍(绿色矢量)。 Normalize 保持向量的方向不变,但给你一个长度(也称为“幅度”)为 1(红色向量)。

在较旧的射击游戏中,您可能会发现一个名为 "bunny hopping" 的错误.我不确定这是否是问题的根源,但我猜是。

关于 vel.magnitude > 0.001:

我们实际上想知道 vel.magnitude > 0。但是 vel.magnitude 是计算的结果,因此可能包含舍入误差。如果您使用浮点值,请始终牢记这一点。检查本身已完成,因为 Normalize 方法需要除以幅度,除以零是邪恶的。不确定 Normalize 是否会自行检查。

关于c# - Unity 2D Top Down Shooter运动问题C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24664904/

相关文章:

c# - 在 WPF 的 UserControl 中显示控件的验证错误模板

c# - 来自 Unity 项目的 WebRequest

java - 当catmull rom样条曲线像libgdx中的amaging线一样移动时,如何平滑catmull rom样条曲线

java - 球围绕 3D 球体运动

c# - 动态创建的 PDF 不在 64 位 IE9 窗口中嵌入/呈现内联

c# - Accessor没有实现?

c# - LINQ to SQL 特性

c# - 将 UnityScript 翻译成 C# : yield & transform. 位置

c# - 统一更改Texture2D格式

actionscript-3 - 用于 Nape 中船舶和子弹的 sensorGroup 和 sensorMask 组合