【发布时间】:2017-10-04 14:24:18
【问题描述】:
我正在用 Unity 做“PlayerMoveKeyBoard”,我使用了这个代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingScript : MonoBehaviour {
// Use this for initialization
void Start () {
double x;
double y;
double z;
double speed;
}
// Update is called once per frame
void Update () {
speed = speed * 0.95;
if (Input.GetKeyDown("Up")) {
speed += 2;
transform.Translate (speed, 0, 0);
}
if (Input.GetKeyDown("Down")) {
speed += 2;
transform.Translate (-speed, 0, 0);
}
if (Input.GetKeyDown("Right")) {
speed += 2;
transform.Translate (0, 0, speed);
}
if (Input.GetKeyDown("Left")) {
speed += 2;
transform.Translate (0, 0, -speed);
}
}
}
但它说:
(17, 11) 当前上下文中不存在名称“speed”
【问题讨论】:
-
从代码给出的权利 - 你还没有声明速度 - 变量只存在于“开始”然后死
-
等等....它需要是浮动的,而不是双倍的
-
float或double对这种错误没有影响 -
Variable scope,学习如何编码的早期课程之一......