Unity3D中的这些错误是什么?

时间:2016-02-11 01:50:58

标签: c# unity3d

我正在进行在线C#和Unity3D课程,但我遇到了问题。我似乎无法找出为什么我会收到所有这些错误。

以下是我收到的错误: http://i.imgur.com/rsRjDh1.png

这是我的代码:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour 
{
    public float speed = 5.0f;
    public float rotateSpeed = 100.0f;
    public bool canMoveSideways = false;

    void Update ()
    {
        Movement ();
    }

    void Movement ()
    {
        if (Input.GetKey (KeyCode.UpArrow)) 
        {
                Debug.Log ("Key UpArrow Pressed.");

                this.transform.Translate (new Vector3 (0, 0, speed * Time.deltaTime));
        } 
        else if (Input.GetKey (KeyCode.DownArrow)) 
        {
                Debug.Log ("Key DownArrow Pressed.");

                this.transform.Translate (new Vector3 (0, 0, -speed * Time.deltaTime));
        }

        if (Input.GetKey (KeyCode.LeftArrow)) 
        {
            if (canMoveSideways) 
            {
                Debug.Log ("Key LeftArrow Pressed.");

                this.transform.Translate (new Vector3 (-speed * Time.deltaTime, 0, 0));
            } 
            else
            {
                Debug.Log ("Key LeftArrow Pressed Rotate");

                this.transform.Rotate (new Vector3 (0, rotateSpeed * Time.deltaTime, 0));
            }
        } 
        else if (Input.GetKey (KeyCode.RightArrow)) 
        {
            if (canMoveSideways) 
            {
                Debug.Log ("Key RightArrow Pressed Rotate");

                this.transform.Rotate (new Vector3 (0, -rotateSpeed * Time.deltaTime, 0));
            }
            else(Input.GetKey (KeyCode.RightArrow))
            {
                Debug.Log ("Key RightArrow Pressed.");

                this.transform.Translate (new Vector3 (speed * Time.deltaTime, 0, 0));
            }
        }

        if (Input.GetKey (KeyCode.W)) 
        {
            Debug.Log ("Key W Pressed.");

            this.transform.Translate (new Vector3 (0, speed * Time.deltaTime, 0));
        }
        else if (Input.GetKey (KeyCode.S)) 
        {
            Debug.Log ("Key S Pressed.");

            this.transform.Translate (new Vector3 (0, -speed * Time.deltaTime, 0));
        }

    }
}

谢谢!

2 个答案:

答案 0 :(得分:0)

第53行有一个语法错误。我猜你忘了删除if子句完成(测试仍在那里)

答案 1 :(得分:0)

if (canMoveSideways) 
{
     Debug.Log ("Key RightArrow Pressed Rotate");
     this.transform.Rotate (new Vector3 (0, -rotateSpeed * Time.deltaTime, 0));
}
else(Input.GetKey (KeyCode.RightArrow))   // here you are missing if
{
     Debug.Log ("Key RightArrow Pressed.");
     this.transform.Translate (new Vector3 (speed * Time.deltaTime, 0, 0));

}

您错过了if或者您应该删除括号内的内容。您还可以简化代码:

 float vert = Input.GetAxis("Vertical");
 this.transform.Translate (new Vector3 (0, 0, speed * Time.deltaTime * vert));
 float hor = Input.GetAxis("Horizontal");

 if (canMoveSideways) {
      this.transform.Translate (new Vector3 (hor * speed * Time.deltaTime, 0, 0));
 } else {
      this.transform.Rotate (new Vector3 (0, hor * rotateSpeed * Time.deltaTime, 0));
 }

不完全确定你在做什么,所以你可能不得不改变一些。我们的想法是检查轴而不是键。它得到相同的结果,但你减少了检查和潜在的错误。如果您需要从0到1平滑或从编辑 - >从0到1捕捉,您也可以控制GetAxis的值。输入设置 - >输入 - >轴 - >卡扣