创建项目
打开 unity hub,点击新项目,选择 3d 模板
创建完毕后是这样

unity 资源商店
可以使用别人做好的素材,AssetStore
找到想要的素材,点击在 unity 中打开,此时编辑器会弹出一个 Package Manager。点击 download 下载,下载好之后可以 import 导入,选择需要的资源导入即可。导入完毕在 Assets 会多一些文件夹
这里导入一个Prototyping Pack (Free)

添加素材
在刚刚的资源中,选择一个扁平地形,这里选的是 PTK_Cuboid_5

添加纹理
在文件夹 materias,选择想要的纹理直接拖到物体上
把地图多个物体作为一个整体
可以右键 create empty,重命名后可以将多个物体拖到这个下,操作整个地图只需要操作这个 empty

删除Main camera
由于该项目是一个FPS(第一人称射击游戏),所以不需要第三视角的相机
创建角色
这里以一个球体来表示玩家,右键添加一个 sphere,重命名为 Player,右键 Player 创建一个 empty,重命名为 Graphics,这个用来管理纹理。在编辑器右侧把Player带 mesh 的组件(这是用来控制纹理的,纹理我们用Graphics来管理)移除掉,右键 remove component。在 graphics 里面添加一个 Sphere,因为外侧的 Player 也是一个 Sphere,删除掉里面 sphere 的碰撞检测组件,Sphere collider。


把相机放到第一视角
在 Player 上右键创建一个 Camera,由于物体的面都是有向的,所以可以把相机塞到球体里面,此时相机可以看到外面,外面看不到相机。

添加玩家的枪
在资源商店选择3D GUNS | Guns Pack。下载并导入

把喜欢的枪添加到相机里面,直接把枪拖到 Player 里的 camera 上。调整枪的方向、大小和位置,第一人称能看到枪

玩家移动
注意一个问题,玩家移动视角时,左右看是玩家左右旋转,而抬头低头就只旋转 Camera。
实现键盘控制玩家移动
给 Player 增加一个 rigidbody 组件,在 Unity 中,Rigidbody 组件用于为游戏对象添加物理属性,使其能够受到物理引擎的控制。由于 Player 是一个球体需要浮空一点,所以关闭 rigidbody 的重力
给 Player 添加代码,选择 Player,add component 一个 new script。添加两个脚本文件: PlayerInput 和 PlayerController。

打开代码后:
PlayerController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { void Start() {
}
void Update() {
} }
|
PlayerInput
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerInput : MonoBehaviour { void Start() {
}
void Update() {
} }
|
获取键盘前后左右的输入,注意带[SerializeField]的变量要在unity editor中赋值,PlayerInput.cs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerInput : MonoBehaviour { [SerializeField] private float speed = 2f;
[SerializeField] private PlayerController controller;
void Start() {
}
void Update() { float xMov = Input.GetAxisRaw("Horizontal");
float yMov = Input.GetAxisRaw("Vertical");
Vector3 velocity = (transform.right * xMov + transform.forward * yMov).normalized * speed;
controller.Move(velocity); } }
|
控制玩家移动的逻辑,PlayerController.cs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { [SerializeField] private Rigidbody rb;
private Vector3 velocity = Vector3.zero;
public void Move(Vector3 _velocity) { velocity = _velocity; }
private void PerformMovement() { if (velocity != Vector3.zero) { rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime); } }
private void FixedUpdate() { PerformMovement(); } }
|
实现旋转视角
PlayerController.cs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { [SerializeField] private Rigidbody rb;
[SerializeField] private Camera cam;
private Vector3 velocity = Vector3.zero;
private Vector3 yRotation = Vector3.zero;
private Vector3 xRotation = Vector3.zero;
public void Move(Vector3 _velocity) { velocity = _velocity; }
public void Rotate(Vector3 _yRotation, Vector3 _xRotation) { yRotation = _yRotation; xRotation = _xRotation; }
private void PerformMovement() { if (velocity != Vector3.zero) { rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime); } }
private void PerformRotation() { if (yRotation != Vector3.zero) { rb.transform.Rotate(yRotation); }
if (xRotation != Vector3.zero) { cam.transform.Rotate(xRotation); } }
private void FixedUpdate() { PerformMovement(); PerformRotation(); } }
|
PlayerInput.cs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerInput : MonoBehaviour { [SerializeField] private float speed = 2f;
[SerializeField] private float lookSensitivity = 8f;
[SerializeField] private PlayerController controller;
void Start() { Cursor.lockState = CursorLockMode.Locked; }
void Update() { float xMov = Input.GetAxisRaw("Horizontal");
float yMov = Input.GetAxisRaw("Vertical");
Vector3 velocity = (transform.right * xMov + transform.forward * yMov).normalized * speed;
controller.Move(velocity);
float xMouse = Input.GetAxisRaw("Mouse X"); float yMouse = Input.GetAxisRaw("Mouse Y");
Vector3 yRotation = new Vector3(0f, xMouse, 0f) * lookSensitivity; Vector3 xRotation = new Vector3(-yMouse, 0f, 0f) * lookSensitivity;
controller.Rotate(yRotation, xRotation); } }
|
导出项目
File->Build Settings->选择平台->PlayerSettings 选择合适选项

这里以 build 一个 Windows 为例:
