Creating Player Movement (Single Player)
创造玩家运动(单人)
The first piece of game-like functionality in this example will be to move the Player GameObject in the scene.
本例中第一个类似游戏的功能是在场景中移动播放器的游戏对象。
We will do this with a new script called “PlayerController”.
我们将使用一个名为“PlayerController”的新脚本实现这一点。
To begin with the PlayerController script will be written without any Networking code so it will only work in a single-player environment.
首先要编写PlayerController脚本,而不需要任何网络代码,因此它只能在一个单人环境中工作。
Create and Add a new script named “PlayerController” to the Player prefab asset.
创建并添加一个名为“PlayerController”的新脚本给播放器预置资产。
Open the script for editing.
打开用于编辑的脚本。
Replace all of the code in the script with the following simple PlayerController class:
将脚本中的所有代码替换为以下简单的PlayerController类:
PlayerController
C#:
using UnityEngine;
public class PlayerController : MonoBehaviour
{
void Update()
{
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
transform.Rotate(0, x, 0);
transform.Translate(0, 0, z);
}
}
It is worth noting that the entire script above can be copied into the clipboard by using the button above the script to the upper right that looks like this:
值得注意的是,上面的整个脚本可以被复制到剪贴板中,上面的按钮是这样的:
This PlayerController script allows the player to control the Player GameObject.
这个PlayerController脚本允许玩家控制玩家的游戏对象。
By default, Input.GetAxis(“Horizontal”) and Input.GetAxis(“Vertical”) allow the player to use the WASD and arrow keys, a controller pad or other device to move the player.
默认情况下,Input.GetAxis(“水平”)和Input.GetAxis(“垂直”)允许玩家使用WASD和箭头键、一个控制器pad或其他设备来移动播放器。
For more information, please see the page on the Input Manager.
有关更多信息,请参见输入管理器上的页面。
Save the script.
保存脚本。
Return to Unity.
回到Unity。
Save the scene.
保存场景。