给摄像机添加CharacterController和MouseLook组件
如图
在存储C#的文件夹中建立Move文件
编写Move文件
using System.Collections;
using UnityEngine;
using System.Collections;
public class Move : MonoBehaviour {
// Use this for initialization
private CharacterController controller;
//private Rigidbody rigidbody;
private float speed = 0.2f;
void Start()
{
//rigidbody = this.GetComponent<Rigidbody>();
controller = this.GetComponent<CharacterController>();
}
void Update()
{
if (Input.GetKey("a"))
controller.Move(transform.right * -speed);
if (Input.GetKey("d"))
controller.Move(transform.right * speed);
if (Input.GetKey("w"))
controller.Move(transform.forward * speed);
if (Input.GetKey("s"))
controller.Move(transform.forward * -speed);
}
}
代码解释将来再补
将Move文件拖拽绑定MainCamera
之后就可以完成主视角第一人称移动的实现