【发布时间】: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;
}
}
控制台没有错误,我的代码也看不到任何错误...
请帮忙!
非常感谢任何答案。
谢谢。
【问题讨论】:
-
“不工作”是什么意思?有什么错误吗?例外?意外行为?