├── FadeInOut.cs ├── README.md ├── Tags.cs ├── fps_FPCamera.cs ├── fps_FPInput.cs ├── fps_Input.cs ├── fps_PlayerControl.cs ├── fps_PlayerHealth.cs ├── fps_PlayerInventory.cs └── fps_PlayerParameter.cs /FadeInOut.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.SceneManagement;//SceneManager使用的命名空间 5 | 6 | public class FadeInOut : MonoBehaviour 7 | { 8 | public float fadeSpeed = 1.5f;//设置渐隐渐现的速度 9 | private bool sceneStarting = true;//设置场景开始,当场景开始时实现渐隐渐现的效果 10 | private GUITexture tex;//设置一个GUITexture类型的组件名为tex 11 | private void Start() 12 | { 13 | tex = this.GetComponent();//给组件进行赋值 14 | tex.pixelInset = new Rect(0, 0, Screen.width, Screen.height);//给组件初始化,位置为0,0,大小和屏幕一致 15 | } 16 | private void Update() 17 | { 18 | if (sceneStarting) 19 | { 20 | StartScene();//实现场景的渐现 21 | } 22 | } 23 | private void FadeToClear()//实现渐现 24 | { 25 | tex.color = Color.Lerp(tex.color, Color.clear, fadeSpeed * Time.deltaTime);//fadeSpeed * Time.deltaTime表示速度 26 | } 27 | private void FadeToBlack()//实现渐隐 28 | { 29 | tex.color = Color.Lerp(tex.color, Color.black, fadeSpeed * Time.deltaTime);//tex.color表示当前的颜色,Color.black表示渐变后的颜色 30 | } 31 | private void StartScene()//渐现开始 32 | { 33 | FadeToClear();//场景开始实现渐现效果 34 | if (tex.color.a <= 0.05f)//判断是否变为透明 35 | { 36 | tex.color = Color.clear;//将当前的颜色变为彻底透明 37 | tex.enabled = false;//禁用GUI组件 38 | sceneStarting = true;//将标识位设置为false 39 | } 40 | } 41 | public void EndScene()//渐隐开始 42 | { 43 | tex.enabled = true; 44 | FadeToBlack(); 45 | if (tex.color.a >= 0.95f) 46 | { 47 | SceneManager.LoadScene("Demo");//进行场景跳转到当前场景,进行重复加载 48 | } 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | *~~@2019072103d~~ * 2 | # 1.导入所需的资源 3 | > 链接:https://pan.baidu.com/s/1kLnu28aPiZ7ak6j4rADqsw 提取码:m71u 4 | # 2.项目开发 5 | ## 2.1标签的封装 6 | > 创建一个名为Tags的脚本,将项目中所有将要使用的标签以const常量的方式进行分装。 7 | ``` 8 | using System.Collections; 9 | using System.Collections.Generic; 10 | using UnityEngine; 11 | 12 | public class Tags{ 13 | public const string player = "Player";//代表玩家 14 | public const string gameController = "GameController";//代表游戏控制器 15 | public const string enemy = "Enemy";//代表敌人 16 | public const string fader = "Fader";//代表淡入淡出的画布 17 | public const string mainCamera = "MainCamera";//代表主摄像机 18 | } 19 | ``` 20 | ## 2.2实现屏幕淡入淡出的效果 21 | > 1.在场景面板中创建一个空物体,命名为FadeInOut,加入组件GUI Texture,color设置为黑色,Texture设置为swatch_black_dff(在导入的资源中搜索) 22 | > 2.为物体FadeInOut创建一个脚本FadeInOut用来实现屏幕的渐隐渐现 23 | 24 | ``` 25 | using System.Collections; 26 | using System.Collections.Generic; 27 | using UnityEngine; 28 | using UnityEngine.SceneManagement;//SceneManager使用的命名空间 29 | 30 | public class FadeInOut : MonoBehaviour 31 | { 32 | public float fadeSpeed = 1.5f;//设置渐隐渐现的速度 33 | private bool sceneStarting = true;//设置场景开始,当场景开始时实现渐隐渐现的效果 34 | private GUITexture tex;//设置一个GUITexture类型的组件名为tex 35 | private void Start() 36 | { 37 | tex = this.GetComponent();//给组件进行赋值 38 | tex.pixelInset = new Rect(0, 0, Screen.width, Screen.height);//给组件初始化,位置为0,0,大小和屏幕一致 39 | } 40 | private void Update() 41 | { 42 | if (sceneStarting) 43 | { 44 | StartScene();//实现场景的渐现 45 | } 46 | } 47 | private void FadeToClear()//实现渐现 48 | { 49 | tex.color = Color.Lerp(tex.color, Color.clear, fadeSpeed * Time.deltaTime);//fadeSpeed * Time.deltaTime表示速度 50 | } 51 | private void FadeToBlack()//实现渐隐 52 | { 53 | tex.color = Color.Lerp(tex.color, Color.black, fadeSpeed * Time.deltaTime);//tex.color表示当前的颜色,Color.black表示渐变后的颜色 54 | } 55 | private void StartScene()//渐现开始 56 | { 57 | FadeToClear();//场景开始实现渐现效果 58 | if (tex.color.a <= 0.05f)//判断是否变为透明 59 | { 60 | tex.color = Color.clear;//将当前的颜色变为彻底透明 61 | tex.enabled = false;//禁用GUI组件 62 | sceneStarting = true;//将标识位设置为false 63 | } 64 | } 65 | public void EndScene()//渐隐开始 66 | { 67 | tex.enabled = true; 68 | FadeToBlack(); 69 | if (tex.color.a >= 0.95f) 70 | { 71 | SceneManager.LoadScene("Demo");//进行场景跳转到当前场景,进行重复加载 72 | } 73 | 74 | } 75 | } 76 | ``` 77 | ## 2.3按键输入封装 78 | 79 | > 在场景面板中创建一个空物体,命名为GameController,将其Tag设置为GameController,同时设置脚本fps_Input 80 | 81 | ``` 82 | using System.Collections; 83 | using System.Collections.Generic; 84 | using UnityEngine; 85 | 86 | public class fps_Input : MonoBehaviour { 87 | public class fps_InputAxis//自定义轴辅助类,存储自定义轴的信息 88 | { 89 | public KeyCode positive;//定义一个正向按键 90 | public KeyCode negative;//定义一个负向按键 91 | } 92 | public Dictionary buttons = new Dictionary();//定义一个集合 93 | public Dictionary axis = new Dictionary();//定义一个自定义轴集合 94 | public List unityAxis = new List();//定义一个unity内部轴的集合 95 | private void Start() 96 | { 97 | SetupDefault(); 98 | } 99 | 100 | private void SetupDefault(string type = "")//设置默认的按键,type中的内容表示你要设置什么类型的东西,是按钮类型的、自定义轴还是uinty默认自带的轴 101 | { 102 | if(type == ""||type == "buttons") 103 | { 104 | if (buttons.Count == 0)//说明按钮没有进行过初始化 105 | { 106 | AddButton("Fire", KeyCode.Mouse0);//鼠标左键设置为开火键 107 | AddButton("Reload", KeyCode.R); 108 | AddButton("Jump", KeyCode.Space); 109 | AddButton("Crouch", KeyCode.C);//蹲下按键 110 | AddButton("Sprint", KeyCode.LeftShift);//加速按键 111 | } 112 | } 113 | if (type == "" || type == "Axis")//初始化自定义轴 114 | { 115 | if (axis.Count == 0) 116 | { 117 | AddAxis("Horizontal", KeyCode.W, KeyCode.S); 118 | AddAxis("Vertical", KeyCode.A, KeyCode.D); 119 | } 120 | } 121 | if (type == "" || type == "UnityAxis")//初始化unity自定义的轴 122 | { 123 | if (unityAxis.Count == 0) 124 | { 125 | AddUnityAxis("Mouse X"); 126 | AddUnityAxis("Mouse Y"); 127 | AddUnityAxis("Horizontal"); 128 | AddUnityAxis("Vertical"); 129 | } 130 | } 131 | } 132 | public void AddButton(string n,KeyCode k)//添加按钮的方法,两个参数表示按钮的名称和键位 133 | { 134 | if (buttons.ContainsKey(n))//判断按钮按键集合中是否包含该按钮 135 | buttons[n] = k;//包含则加入键位 136 | else 137 | buttons.Add(n, k);//不包含则加入这个按钮和键位 138 | } 139 | private void AddAxis(string n,KeyCode pk,KeyCode nk)//添加自定义轴的方法,三个参数代表自定义轴的名称,正方向和负方向 140 | { 141 | if (axis.ContainsKey(n)) 142 | { 143 | axis[n] = new fps_InputAxis() { positive = pk, negative = nk }; 144 | } 145 | else 146 | { 147 | axis.Add(n, new fps_InputAxis() { positive = pk, negative = nk }); 148 | } 149 | } 150 | private void AddUnityAxis(string n)//添加unity自定义轴的方法 151 | { 152 | if (!unityAxis.Contains(n)) 153 | { 154 | unityAxis.Add(n); 155 | } 156 | } 157 | //外界获取调用的方法 158 | public bool GetButton(string button) 159 | { 160 | if (buttons.ContainsKey(button)) 161 | { 162 | return Input.GetKey(buttons[button]);//外界获取按钮的方法,只要按键没有被抬起就一直调用 163 | } 164 | return false; 165 | } 166 | public bool GetButtonDown(string button) 167 | { 168 | if (buttons.ContainsKey(button)) 169 | { 170 | return Input.GetKeyDown(buttons[button]);//外界获取按钮按下的方法 171 | } 172 | return false; 173 | } 174 | public float GetAxis(string axis)//轴返回的值在-1到1之间,外界获取自定义轴的方法 175 | { 176 | if (this.unityAxis.Contains(axis)) 177 | { 178 | return Input.GetAxis(axis); 179 | } 180 | else 181 | return 0; 182 | } 183 | public float GetAxisRaw(string axis)//这里只返回三个值,-1,0,1 184 | { 185 | if (this.axis.ContainsKey(axis))//判断自定义轴是否包含这个轴 186 | { 187 | float val = 0; 188 | if (Input.GetKey(this.axis[axis].positive)) 189 | return 1;//按下正向键返回1 190 | if (Input.GetKey(this.axis[axis].negative)) 191 | return -1; 192 | return val; 193 | } 194 | else if (unityAxis.Contains(axis))//如果自定义的轴不包含这个轴,就去unity自定义的轴去找 195 | { 196 | return Input.GetAxisRaw(axis); 197 | } 198 | else//都没有找到该轴 199 | { 200 | return 0; 201 | } 202 | } 203 | } 204 | ``` 205 | ## 2.4用户输入的参数封装及赋值 206 | >1.创建一个空物体,作为我们的主角,重命名为FP_Player,将其位置设置为X:16.44,Z:-45.65,加入一个组件character controller(角色控制器),将参数Center中的Y改为1,Radius改为0.4,后期所有的主角行为都通过角色控制器来操作。 207 | >2.书写一个脚本表示用户的输入状态,命名为fps_PlayerParameter,将脚本拖动到FP_Player中 208 | 209 | ``` 210 | using System.Collections; 211 | using System.Collections.Generic; 212 | using UnityEngine; 213 | [RequireComponent(typeof(CharacterController))] 214 | public class fps_PlayerParameter : MonoBehaviour 215 | { 216 | [HideInInspector]//以下操作是通过用户的输入来操作,不需要显示在用户面板中,因此就隐藏属性 217 | public Vector2 inputSmoothLook;//定义一个二维向量表示鼠标的输入 218 | [HideInInspector] 219 | public Vector2 inputMoveVector;//定义一个二维向量表示按键的输入 220 | [HideInInspector] 221 | public bool inputCrouch;//是否下蹲 222 | [HideInInspector] 223 | public bool inputJump;//是否跳跃 224 | [HideInInspector] 225 | public bool inputSprint;//是否冲刺 226 | [HideInInspector] 227 | public bool inputFire;//是否开火 228 | [HideInInspector] 229 | public bool inputReload;//是否装弹 230 | } 231 | ``` 232 | >3.创建一个脚本,为上述定义的变量进行赋值,命名为fps_FPInput,拖动到FP_Player中 233 | ``` 234 | using System.Collections; 235 | using System.Collections.Generic; 236 | using UnityEngine; 237 | 238 | public class fps_FPInput : MonoBehaviour 239 | { 240 | public bool LockCursor//由于是第一人称视角,不需要显示光标,锁定光标 241 | { 242 | get { return Cursor.lockState == CursorLockMode.Locked ? true : false; }//锁定返回true,反则返回false 243 | set 244 | { 245 | Cursor.visible = value;//设定一个鼠标可见的变量 246 | Cursor.lockState = value ? CursorLockMode.Locked : CursorLockMode.None; 247 | } 248 | } 249 | private fps_PlayerParameter parameter;//给脚本的参数进行赋值 250 | private fps_Input input; 251 | void Start() 252 | { 253 | LockCursor = true; 254 | parameter = this.GetComponent();//赋值parameter,通过该脚本所在物体中找到组件fps_PlayerParameter 255 | input = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent();//赋值input,通过游戏物体的标签区寻找组件fps_Input 256 | } 257 | private void Update() 258 | { 259 | InitialInput(); 260 | } 261 | private void InitialInput()//对变量进行赋值 262 | { 263 | parameter.inputMoveVector = new Vector2(input.GetAxis("Horizontal"), input.GetAxis("Vertical")); 264 | parameter.inputSmoothLook = new Vector2(input.GetAxisRaw("Mouse X"), input.GetAxisRaw("Mouse Y"));//通过鼠标控制相机进行旋转来达到视觉旋转的特效 265 | parameter.inputCrouch = input.GetButton("Crouch"); 266 | parameter.inputJump = input.GetButton("Jump"); 267 | parameter.inputFire = input.GetButton("Fire"); 268 | parameter.inputSprint = input.GetButton("Sprint"); 269 | parameter.inputReload = input.GetButton("Reload"); 270 | } 271 | } 272 | ``` 273 | 274 | 275 | -------------------------------------------------------------------------------- /Tags.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Tags{ 6 | public const string player = "Player";//代表玩家 7 | public const string gameController = "GameController";//代表游戏控制器 8 | public const string enemy = "Enemy";//代表敌人 9 | public const string fader = "Fader";//代表淡入淡出的画布 10 | public const string mainCamera = "MainCamera";//代表主摄像机 11 | } 12 | 13 | -------------------------------------------------------------------------------- /fps_FPCamera.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | [RequireComponent(typeof(Camera))]//针对相机的操纵 5 | public class fps_FPCamera : MonoBehaviour { 6 | public Vector2 mouseLookSensitivity = new Vector2(5, 5);//调整鼠标灵敏度 7 | public Vector2 rotationXLimit = new Vector2(87, -87);//相机旋转的垂直方向的角度范围 8 | public Vector2 rotationYLimit = new Vector2(-360, 360);//相机旋转的水平方向的角度范围 9 | public Vector3 positionOffset = new Vector3(0, 2,-0.2f);// 10 | private Vector2 currentMouseLook = Vector2.zero; 11 | private float x_Angle = 0;//x轴旋转角度默认为0 12 | private float y_Angle = 0; 13 | private fps_PlayerParameter parameter;//获取鼠标的输入 14 | private Transform m_Transform;//避免重复调用 15 | private void Start() 16 | { 17 | parameter = GameObject.FindGameObjectWithTag(Tags.player).GetComponent(); 18 | m_Transform = transform; 19 | m_Transform.localPosition = positionOffset; 20 | } 21 | private void Update() 22 | { 23 | UpdateInput(); 24 | } 25 | private void LateUpdate()//旋转处理 26 | { 27 | Quaternion xQuaternion = Quaternion.AngleAxis(y_Angle, Vector3.up);//对应y轴的旋转 28 | Quaternion yQuaternino = Quaternion.AngleAxis(0, Vector3.left);//对应x轴的旋转 29 | m_Transform.parent.rotation = xQuaternion * yQuaternino; 30 | yQuaternino = Quaternion.AngleAxis(-x_Angle, Vector3.left); 31 | m_Transform.rotation = xQuaternion * yQuaternino; 32 | } 33 | private void UpdateInput() 34 | { 35 | if (parameter.inputSmoothLook == Vector2.zero)//表示没有用户输入 36 | return; 37 | GetMouseLock();//用户有输入获取用户输入 38 | y_Angle += currentMouseLook.x;//前者的y表示y轴的旋转,后者的x表示鼠标的输入轴向,鼠标的轴向对应视图中y轴的旋转 39 | x_Angle += currentMouseLook.y; 40 | y_Angle = y_Angle < -360 ? y_Angle += 360 : y_Angle; 41 | y_Angle = y_Angle > 360 ? y_Angle -= 360 : y_Angle;//保证y轴的旋转保持在一定范围内 42 | y_Angle = Mathf.Clamp(y_Angle, rotationYLimit.x, rotationYLimit.y);// 43 | x_Angle = x_Angle < -360 ? x_Angle += 360 : x_Angle; 44 | x_Angle = x_Angle > 360 ? x_Angle -= 360 : x_Angle; 45 | x_Angle = Mathf.Clamp(x_Angle, -rotationXLimit.x, -rotationXLimit.y); 46 | } 47 | private void GetMouseLock (){//对用户输入进行处理 48 | currentMouseLook.x = parameter.inputSmoothLook.x; 49 | currentMouseLook.y = parameter.inputSmoothLook.y; 50 | currentMouseLook.x *= mouseLookSensitivity.x; 51 | currentMouseLook.y *= mouseLookSensitivity.y; 52 | currentMouseLook.y *= -1;//修改y轴的方向 53 | } 54 | } 55 | 56 | -------------------------------------------------------------------------------- /fps_FPInput.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class fps_FPInput : MonoBehaviour 6 | { 7 | public bool LockCursor//由于是第一人称视角,不需要显示光标,锁定光标 8 | { 9 | get { return Cursor.lockState == CursorLockMode.Locked ? true : false; }//锁定返回true,反则返回false 10 | set 11 | { 12 | Cursor.visible = value;//设定一个鼠标可见的变量 13 | Cursor.lockState = value ? CursorLockMode.Locked : CursorLockMode.None; 14 | } 15 | } 16 | private fps_PlayerParameter parameter;//给脚本的参数进行赋值 17 | private fps_Input input; 18 | void Start() 19 | { 20 | LockCursor = true; 21 | parameter = this.GetComponent();//赋值parameter,通过该脚本所在物体中找到组件fps_PlayerParameter 22 | input = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent();//赋值input,通过游戏物体的标签区寻找组件fps_Input 23 | } 24 | private void Update() 25 | { 26 | InitialInput(); 27 | } 28 | private void InitialInput()//对变量进行赋值 29 | { 30 | parameter.inputMoveVector = new Vector2(input.GetAxis("Horizontal"), input.GetAxis("Vertical")); 31 | parameter.inputSmoothLook = new Vector2(input.GetAxisRaw("Mouse X"), input.GetAxisRaw("Mouse Y"));//通过鼠标控制相机进行旋转来达到视觉旋转的特效 32 | parameter.inputCrouch = input.GetButton("Crouch"); 33 | parameter.inputJump = input.GetButton("Jump"); 34 | parameter.inputFire = input.GetButton("Fire"); 35 | parameter.inputSprint = input.GetButton("Sprint"); 36 | parameter.inputReload = input.GetButton("Reload"); 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /fps_Input.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class fps_Input : MonoBehaviour { 6 | public class fps_InputAxis//自定义轴辅助类,存储自定义轴的信息 7 | { 8 | public KeyCode positive;//定义一个正向按键 9 | public KeyCode negative;//定义一个负向按键 10 | } 11 | public Dictionary buttons = new Dictionary();//定义一个集合 12 | public Dictionary axis = new Dictionary();//定义一个自定义轴集合 13 | public List unityAxis = new List();//定义一个unity内部轴的集合 14 | private void Start() 15 | { 16 | SetupDefault(); 17 | } 18 | 19 | private void SetupDefault(string type = "")//设置默认的按键,type中的内容表示你要设置什么类型的东西,是按钮类型的、自定义轴还是uinty默认自带的轴 20 | { 21 | if(type == ""||type == "buttons") 22 | { 23 | if (buttons.Count == 0)//说明按钮没有进行过初始化 24 | { 25 | AddButton("Fire", KeyCode.Mouse0);//鼠标左键设置为开火键 26 | AddButton("Reload", KeyCode.R); 27 | AddButton("Jump", KeyCode.Space); 28 | AddButton("Crouch", KeyCode.C);//蹲下按键 29 | AddButton("Sprint", KeyCode.LeftShift);//加速按键 30 | } 31 | } 32 | if (type == "" || type == "Axis")//初始化自定义轴 33 | { 34 | if (axis.Count == 0) 35 | { 36 | AddAxis("Horizontal", KeyCode.W, KeyCode.S); 37 | AddAxis("Vertical", KeyCode.A, KeyCode.D); 38 | } 39 | } 40 | if (type == "" || type == "UnityAxis")//初始化unity自定义的轴 41 | { 42 | if (unityAxis.Count == 0) 43 | { 44 | AddUnityAxis("Mouse X"); 45 | AddUnityAxis("Mouse Y"); 46 | AddUnityAxis("Horizontal"); 47 | AddUnityAxis("Vertical"); 48 | } 49 | } 50 | } 51 | public void AddButton(string n,KeyCode k)//添加按钮的方法,两个参数表示按钮的名称和键位 52 | { 53 | if (buttons.ContainsKey(n))//判断按钮按键集合中是否包含该按钮 54 | buttons[n] = k;//包含则加入键位 55 | else 56 | buttons.Add(n, k);//不包含则加入这个按钮和键位 57 | } 58 | private void AddAxis(string n,KeyCode pk,KeyCode nk)//添加自定义轴的方法,三个参数代表自定义轴的名称,正方向和负方向 59 | { 60 | if (axis.ContainsKey(n)) 61 | { 62 | axis[n] = new fps_InputAxis() { positive = pk, negative = nk }; 63 | } 64 | else 65 | { 66 | axis.Add(n, new fps_InputAxis() { positive = pk, negative = nk }); 67 | } 68 | } 69 | private void AddUnityAxis(string n)//添加unity自定义轴的方法 70 | { 71 | if (!unityAxis.Contains(n)) 72 | { 73 | unityAxis.Add(n); 74 | } 75 | } 76 | //外界获取调用的方法 77 | public bool GetButton(string button) 78 | { 79 | if (buttons.ContainsKey(button)) 80 | { 81 | return Input.GetKey(buttons[button]);//外界获取按钮的方法,只要按键没有被抬起就一直调用 82 | } 83 | return false; 84 | } 85 | public bool GetButtonDown(string button) 86 | { 87 | if (buttons.ContainsKey(button)) 88 | { 89 | return Input.GetKeyDown(buttons[button]);//外界获取按钮按下的方法 90 | } 91 | return false; 92 | } 93 | public float GetAxis(string axis)//轴返回的值在-1到1之间,外界获取自定义轴的方法 94 | { 95 | if (this.unityAxis.Contains(axis)) 96 | { 97 | return Input.GetAxis(axis); 98 | } 99 | else 100 | return 0; 101 | } 102 | public float GetAxisRaw(string axis)//这里只返回三个值,-1,0,1 103 | { 104 | if (this.axis.ContainsKey(axis))//判断自定义轴是否包含这个轴 105 | { 106 | float val = 0; 107 | if (Input.GetKey(this.axis[axis].positive)) 108 | return 1;//按下正向键返回1 109 | if (Input.GetKey(this.axis[axis].negative)) 110 | return -1; 111 | return val; 112 | } 113 | else if (unityAxis.Contains(axis))//如果自定义的轴不包含这个轴,就去unity自定义的轴去找 114 | { 115 | return Input.GetAxisRaw(axis); 116 | } 117 | else//都没有找到该轴 118 | { 119 | return 0; 120 | } 121 | } 122 | } 123 | 124 | -------------------------------------------------------------------------------- /fps_PlayerControl.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | public enum PlayerState//设立枚举表示玩家的状态 5 | { 6 | None,//无状态 7 | Idle,//站立状态 8 | Walk,//行走状态 9 | Crouch,//蹲伏状态 10 | Run,//行走状态 11 | } 12 | public class fps_PlayerControl : MonoBehaviour 13 | { 14 | private PlayerState state = PlayerState.None;//设置默认状态为无状态 15 | public PlayerState State 16 | { 17 | get 18 | { 19 | if (runing) 20 | state = PlayerState.Run; 21 | else if (walking) 22 | state = PlayerState.Walk; 23 | else if (crouching) 24 | state = PlayerState.Crouch; 25 | else 26 | state = PlayerState.Idle; 27 | return state; 28 | } 29 | } 30 | public float sprintSpeed = 10.0f;//设置冲刺状态的相关速度 31 | public float sprintJumpSpeed = 8.0f; 32 | public float normalSpeed = 6.0f;//设置正常状态下的相关速度 33 | public float normalJumpSpeed = 7.0f; 34 | public float crouchSpeed = 2.0f; 35 | public float crouchJumpSpeed = 5.0f; 36 | public float crouchDeltaHeight = 0.5f;//定义玩家蹲伏的时候下降的高度 37 | public float gravity = 20.0f;//定义一个重力加速度 38 | public float cameraMoveSpheed = 8.0f; 39 | public AudioClip jumpAudio; 40 | private float speed;//代表玩家当前的速度 41 | private float jumpSpeed; 42 | private Transform mainCamera;//代表相机 43 | private float standardCamHeight;//存储相机的标准高度 44 | private float crouchingCamHeight;//存储蹲伏时相机的高度 45 | private bool grounded = false;//表示玩家是否在地面 46 | private bool walking = false; 47 | private bool crouching = false; 48 | private bool stopCrouching = false; 49 | private bool runing = false; 50 | private Vector3 normalControllerCenter = Vector3.zero; 51 | private float normalControllerHeight = 0.0f; 52 | private float timer = 0;//计时器默认为零 53 | private CharacterController controller; 54 | private AudioSource audioSource;//播放角色的脚步声 55 | private fps_PlayerParameter parameter; 56 | private Vector3 moveDirection = Vector3.zero; 57 | private void Start() 58 | { 59 | crouching = false;//初始化操作 60 | walking = false; 61 | crouching = false; 62 | runing = false; 63 | jumpSpeed = normalSpeed;//初始化跳跃速度 64 | mainCamera = GameObject.FindGameObjectWithTag(Tags.mainCamera).transform; 65 | standardCamHeight = mainCamera.localPosition.y;//存储相机的标准高度 66 | crouchingCamHeight = standardCamHeight - crouchDeltaHeight;//蹲伏的相机高度 67 | audioSource = this.GetComponent(); 68 | controller = this.GetComponent(); 69 | parameter = this.GetComponent(); 70 | normalControllerCenter = controller.center; 71 | normalControllerHeight = controller.height;//设定正常情况下的高度 72 | } 73 | public void FixedUpdate() 74 | { 75 | UpdateMove(); 76 | AudioManagement(); 77 | } 78 | private void UpdateMove()//操作人物移动 79 | { 80 | if (grounded) 81 | { 82 | moveDirection = new Vector3(parameter.inputMoveVector.x, 0, parameter.inputMoveVector.y); 83 | moveDirection = transform.TransformDirection(moveDirection);//将存在在世界坐标系的方向转变为自身坐标系中的方向 84 | moveDirection *= speed; 85 | if (parameter.inputJump) 86 | { 87 | moveDirection.y = jumpSpeed; 88 | AudioSource.PlayClipAtPoint(jumpAudio, transform.position); 89 | CurrentSpeed(); 90 | } 91 | } 92 | moveDirection.y -= gravity * Time.deltaTime;//表示跳跃后会下落 93 | CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime); 94 | grounded = (flags & CollisionFlags.CollidedBelow) != 0; 95 | if (Mathf.Abs(moveDirection.x) > 0 && grounded || Mathf.Abs(moveDirection.x) > 0 && grounded) 96 | { 97 | if (parameter.inputSprint) 98 | { 99 | walking = false; 100 | runing = true; 101 | crouching = false; 102 | } 103 | else if (parameter.inputCrouch) 104 | { 105 | crouching = true; 106 | walking = false; 107 | runing = false; 108 | } 109 | else 110 | { 111 | walking = true; 112 | crouching = false; 113 | runing = false; 114 | } 115 | } 116 | else 117 | { 118 | if (walking) 119 | walking = true; 120 | if (runing) 121 | runing = true; 122 | if (parameter.inputCrouch) 123 | crouching = true; 124 | else 125 | crouching = false; 126 | } 127 | if (crouching) 128 | { 129 | controller.height = normalControllerHeight - crouchDeltaHeight; 130 | controller.center = normalControllerCenter - new Vector3(0, crouchDeltaHeight / 2, 0); 131 | } 132 | else 133 | { 134 | controller.height = normalControllerHeight; 135 | controller.center = normalControllerCenter; 136 | } 137 | UpdateCrouch(); 138 | CurrentSpeed(); 139 | } 140 | private void CurrentSpeed()//赋值速度 141 | { 142 | switch (State) 143 | { 144 | case PlayerState.Idle: 145 | speed = normalSpeed; 146 | jumpSpeed = normalJumpSpeed; 147 | break; 148 | case PlayerState.Crouch: 149 | speed = crouchSpeed; 150 | jumpSpeed = crouchJumpSpeed; 151 | break; 152 | case PlayerState.Run: 153 | speed = sprintSpeed; 154 | jumpSpeed = sprintJumpSpeed; 155 | break; 156 | case PlayerState.Walk: 157 | speed = normalSpeed; 158 | jumpSpeed = normalJumpSpeed; 159 | break; 160 | } 161 | } 162 | private void AudioManagement()//设置脚步声操作 163 | { 164 | if (State == PlayerState.Walk) 165 | { 166 | audioSource.pitch = 1.0f; 167 | if (!audioSource.isPlaying) 168 | audioSource.Play(); 169 | } 170 | else if (State == PlayerState.Run) 171 | { 172 | audioSource.pitch = 1.3f; 173 | if (!audioSource.isPlaying) 174 | audioSource.Play(); 175 | } 176 | else 177 | { 178 | audioSource.Stop(); 179 | } 180 | } 181 | private void UpdateCrouch()//蹲伏操作过程中相机的相关操作 182 | { 183 | if (crouching) 184 | { 185 | if (mainCamera.localPosition.y > crouchingCamHeight) 186 | { 187 | if (mainCamera.localPosition.y - (crouchDeltaHeight * Time.deltaTime * cameraMoveSpheed) < crouchingCamHeight) 188 | mainCamera.localPosition = new Vector3(mainCamera.localPosition.x, crouchingCamHeight, mainCamera.localPosition.z); 189 | else 190 | mainCamera.localPosition -= new Vector3(0, crouchDeltaHeight * Time.deltaTime * cameraMoveSpheed, 0); 191 | } 192 | else 193 | mainCamera.localPosition = new Vector3(mainCamera.localPosition.x, crouchingCamHeight, mainCamera.localPosition.z); 194 | } 195 | else 196 | { 197 | if (mainCamera.localPosition.y < standardCamHeight) 198 | { 199 | if (mainCamera.localPosition.y + (crouchDeltaHeight * Time.deltaTime * cameraMoveSpheed) > standardCamHeight) 200 | { 201 | mainCamera.localPosition = new Vector3(mainCamera.localPosition.x, standardCamHeight, mainCamera.localPosition.x); 202 | } 203 | else 204 | { 205 | mainCamera.localPosition = new Vector3(0, crouchDeltaHeight * Time.deltaTime * cameraMoveSpheed, 0); 206 | } 207 | } 208 | else 209 | { 210 | mainCamera.localPosition = new Vector3(mainCamera.localPosition.x, standardCamHeight, mainCamera.localPosition.x); 211 | } 212 | } 213 | 214 | } 215 | 216 | } 217 | -------------------------------------------------------------------------------- /fps_PlayerHealth.cs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yuconsecration/CrossingTheLineOfFire/97f4977381cc6485a318d7df8745e1a799cc9933/fps_PlayerHealth.cs -------------------------------------------------------------------------------- /fps_PlayerInventory.cs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yuconsecration/CrossingTheLineOfFire/97f4977381cc6485a318d7df8745e1a799cc9933/fps_PlayerInventory.cs -------------------------------------------------------------------------------- /fps_PlayerParameter.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | [RequireComponent(typeof(CharacterController))] 5 | public class fps_PlayerParameter : MonoBehaviour 6 | { 7 | [HideInInspector]//以下操作是通过用户的输入来操作,不需要显示在用户面板中,因此就隐藏属性 8 | public Vector2 inputSmoothLook;//定义一个二维向量表示鼠标的输入 9 | [HideInInspector] 10 | public Vector2 inputMoveVector;//定义一个二维向量表示按键的输入 11 | [HideInInspector] 12 | public bool inputCrouch;//是否下蹲 13 | [HideInInspector] 14 | public bool inputJump;//是否跳跃 15 | [HideInInspector] 16 | public bool inputSprint;//是否冲刺 17 | [HideInInspector] 18 | public bool inputFire;//是否开火 19 | [HideInInspector] 20 | public bool inputReload;//是否装弹 21 | 22 | } 23 | 24 | --------------------------------------------------------------------------------