【问题标题】:Why does my code not run in Unity? Unity C#为什么我的代码不能在 Unity 中运行?统一 C#
【发布时间】:2019-01-26 01:50:18
【问题描述】:

由于某种原因,我的代码无法正常工作...我不知道为什么。有人可以帮忙吗?我正在使用 switch 语句来控制我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        Vector3 pos = transform.position;
        string state = "idle";
        float vx = 0f;
        float vy = 0f;
        float playerSpeed = 2f * Time.deltaTime;

        switch (state) {
            case "idle": 
                vx = 0;
                vy = 0;

                if (Input.GetKey (KeyCode.A)) state = "left";
                if (Input.GetKey (KeyCode.D)) state = "right";
                if (!Input.GetKey (KeyCode.D) && !Input.GetKey (KeyCode.A)) state = "idle";             
                break;

            case "left":
                vx = -1 * playerSpeed;
                vy = 0;

                if (Input.GetKey (KeyCode.A)) state = "left";
                if (Input.GetKey (KeyCode.D)) state = "right";
                if (!Input.GetKey (KeyCode.D) && !Input.GetKey (KeyCode.A)) state = "idle";             
                break;

            case "right":
                vx = playerSpeed;
                vy = 0;

                if (Input.GetKey (KeyCode.A)) state = "left";
                if (Input.GetKey (KeyCode.D)) state = "right";
                if (!Input.GetKey (KeyCode.D) && !Input.GetKey (KeyCode.A)) state = "idle";             
                break;
        }
        vx += pos.x;
        vy += pos.y;
        pos += transform.position;

    }
}

控制台没有错误,我的代码也看不到任何错误...

请帮忙!

非常感谢任何答案。

谢谢。

【问题讨论】:

  • “不工作”是什么意思?有什么错误吗?例外?意外行为?

标签: c# unity3d scripting


【解决方案1】:

您正在评估每个 switch 案例中的输入,而不是在评估 switch 之前。您还要检查输入,然后检查是否缺少输入,因此只需使用 else 来清理这些检查。除了vy = 0,你什么也没有做,所以不要费心设置:

if (Input.GetKey (KeyCode.A))
    state = "left";
else if (Input.GetKey (KeyCode.D)) // if you hold both A and D, A will get priority
    state = "right";
else
    state = "idle";
switch(state)
{
    case("idle")
        vx = 0;
        break;
    case("left")
        vx = playerSpeed;
        break;
    case("right")
        vx = -1 * playerSpeed;
        break;
}

您也没有正确地将值添加到转换的位置,您只是将它们添加到临时变量pos(您根本不需要的变量):

vx += pos.x;
vy += pos.y;
pos += transform.position;

应该是:

transform.position.Translate(vx, vy, 0);

我还想指出,开关本身完全没有意义,但我将此作为我的答案,所以很清楚做错了什么;您应该在 if/else if/else 语句中设置您的 vx 值。

【讨论】:

    【解决方案2】:

    你看,我觉得“状态”每次都被重置为“空闲”。你能试着移动那个string state = "idle"; Start() 函数吗 然后将此state = "idle"; 添加到其他地方,例如在

    的底部
       vx += pos.x;
       vy += pos.y;
       pos += transform.position;
    

    你看到你有一个开关盒。它将通过代码一次,你看到那些中断了吗?这就是为什么他们不输入或接收您的 vx 和 vy 上的值更改。我建议您只使用 If Else 而不是开关来检测输入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      • 2015-01-21
      相关资源
      最近更新 更多