C# 如何使用屏幕上的按钮使我的播放器移动?

C# 如何使用屏幕上的按钮使我的播放器移动?,c#,object,unity3d,C#,Object,Unity3d,正如标题所说,我如何在与C#的统一中做到这一点?我自己也尝试过一些东西,但我尝试过的东西总是让我的播放器在屏幕上闪闪发光。所以我想知道应该怎么做。 -PlayerScript.cs -MoveScript.cs(我不知道这个是否对你们有用) 这里是我的GameButtons.cs,我试图在其中制作按钮(不介意评论,我喜欢复制和粘贴): 你的场景有多大?有没有可能是因为速度太快,玩家正在离开屏幕 顺便说一下,您的“向上”输入正在修改x而不是y 我相机的刻度是1,1,1。我的视口矩形是宽度1,高度1

正如标题所说,我如何在与C#的统一中做到这一点?我自己也尝试过一些东西,但我尝试过的东西总是让我的播放器在屏幕上闪闪发光。所以我想知道应该怎么做。 -PlayerScript.cs -MoveScript.cs(我不知道这个是否对你们有用) 这里是我的GameButtons.cs,我试图在其中制作按钮(不介意评论,我喜欢复制和粘贴):


你的场景有多大?有没有可能是因为速度太快,玩家正在离开屏幕


顺便说一下,您的“向上”输入正在修改x而不是y

我相机的刻度是1,1,1。我的视口矩形是宽度1,高度1,如果有帮助的话。这就是问题所在,我不知道速度是否太高,因为我不知道它有多高。关于向上的x:这对我来说并不重要,首先我想看看它是否做了什么。好的,在这个例子中,我们并不知道系统的行为。尝试执行“speed.x+=Vector.left*1”或“speed.x+=Vector.left*0.1f”,看看会发生什么。然后我得到:名称“Vector”在当前上下文中不存在。当我使用vector2时,我得到:“UnityEngine.vector2”不包含“left”的定义。Vector3:运算符“+=”不能应用于“float”和“UnityEngine.Vector3”类型的操作数哎呀,对不起,我的意思是使用现有行并使用“*1”或“*0.1f”而不是“*10”。放慢速度,看看是否有任何动作导致你的播放器传送到屏幕外,或者速度太快。我修改的原始代码是speed.y+=Vector2.down*1;但后来我发现:“UnityEngine.Vector2”不包含“down”的定义
using UnityEngine;

public class GameButtons : MonoBehaviour
{
    void OnGUI()
  {
    const int buttonWidth = 60;
    const int buttonHeight = 60;

    // Draw a button to start the game
    if (
      GUI.Button(
        // Center in X, 2/3 of the height in Y
        new Rect(
          Screen.width / 5 - (buttonWidth / 2),
          (3 * Screen.height / 4) - (buttonHeight / 2),
          buttonWidth,
          buttonHeight
        ),
        "UP"
      )
    )
    {
      // On Click, load the first level.
      // "Stage1" is the name of the first scene we created.
      speed.x += Vector2.up * 10;
    }
        if (
      GUI.Button(
        // Center in X, 2/3 of the height in Y
        new Rect(
         Screen.width / 5 - (buttonWidth / 2),
          (13 * Screen.height / 14) - (buttonHeight / 2),
          buttonWidth,
          buttonHeight
        ),
        "DOWN"
      )
    )
    {
      // On Click, load the first level.
      // "Stage1" is the name of the first scene we created.
      speed.y += Vector2.down * 10;
    }
        if (
      GUI.Button(
        // Center in X, 2/3 of the height in Y
        new Rect(
          Screen.width / 8 - (buttonWidth / 2),
          (5 * Screen.height / 6) - (buttonHeight / 2),
          buttonWidth,
          buttonHeight
        ),
        "LEFT"
      )
    )
    {
      speed.x += Vector2.left * 10;
    }
        if (
      GUI.Button(
        // Center in X, 2/3 of the height in Y
        new Rect(
          Screen.width / 4 - (buttonWidth / 2) + 24,
          (5 * Screen.height / 6) - (buttonHeight / 2),
          buttonWidth,
         buttonHeight
        ),
        "RIGHT"
      )
    )
    {
      // On Click, load the first level.
      // "Stage1" is the name of the first scene we created.
      speed.y += Vector2.right * 10;
    }
        if (
      GUI.Button(
        // Center in X, 2/3 of the height in Y
        new Rect(
          (Screen.width / 1) - (buttonWidth * 2) - 20,
          (5 * Screen.height / 6) - (buttonHeight / 2),
          buttonWidth,
          buttonHeight
        ),
        "FIRE"
      )
    )
    {
      // On Click, load the first level.
      // "Stage1" is the name of the first scene we created.
      Application.LoadLevel("ShootEmUp");
    }
  }
}