using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
private Transform m_Transform;
void Start () {
m_Transform = gameObject.GetComponent<Transform>();
}
// Update is called once per frame
void Update () {
MoveControl();
}
void MoveControl()
{
if (Input.GetKey(KeyCode.W))
{
m_Transform.Translate(Vector3.forward * 0.1f, Space.Self);
}
if (Input.GetKey(KeyCode.S))
{
m_Transform.Translate(Vector3.back * 0.1f, Space.Self);
}
if (Input.GetKey(KeyCode.A))
{
m_Transform.Translate(Vector3.left * 0.1f, Space.Self);
}
if (Input.GetKey(KeyCode.D))
{
m_Transform.Translate(Vector3.right * 0.1f, Space.Self);
}
if (Input.GetKey(KeyCode.Q))
{
m_Transform.Rotate(Vector3.up, -1.0f);
}
if (Input.GetKey(KeyCode.E))
{
m_Transform.Rotate(Vector3.up, 1.0f);
}
m_Transform.Rotate(Vector3.up, Input.GetAxis("Mouse X"));
m_Transform.Rotate(Vector3.left, Input.GetAxis("Mouse Y"));
}
}
回到Unity3D,点击"运行"按钮,发现原来的Scene面板会切换到Game(游戏预览)面板。这时,就可以通过WSAD键控制立方体的前后左右移动,Q和E键控制立方体左右旋转。同时,鼠标前后和左右移动可以控制物体上下及左右旋转了。