耗時一周制作的第一人稱射擊遊戲,希望能幫助到大家!
由于代碼較多,分為三篇展示,感興趣的朋友們可以點選檢視!
Unity3D FPS Game:第一人稱射擊遊戲(一)
Unity3D FPS Game:第一人稱射擊遊戲(二)
Unity3D FPS Game:第一人稱射擊遊戲(三)
文章目錄
- 遊戲展示
- 資源
- 代碼
-
- AnimatorSetup.cs
- FadeInOut.cs
- FPS_Camera.cs
- FPS_CrossHair.cs
遊戲展示

資源
連結:https://pan.baidu.com/s/15s8KSN_4JPAvD_fA8OIiCg
提取碼:1vod
代碼
AnimatorSetup.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimatorSetup
{
public float speedDampTime = 0.1f; //速度阻尼時間
public float angularSpeedDampTime = 0.7f; //角速度阻尼時間
public float angularResponseTime = 1f; //角度反應時間
private Animator animator;
private HashIDs hashIDs;
public AnimatorSetup(Animator animator ,HashIDs hashIDs)
{
this.animator = animator;
this.hashIDs = hashIDs;
}
public void Setup(float speed,float angular)
{
float angularSpeed = angular / angularResponseTime;
animator.SetFloat(hashIDs.speedFloat, speed, speedDampTime, Time.deltaTime);
animator.SetFloat(hashIDs.angularSpeedFloat, angularSpeed, angularSpeedDampTime, Time.deltaTime);
}
}
FadeInOut.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
/// <summary>
/// 漸隐漸現效果
/// </summary>
public class FadeInOut : MonoBehaviour
{
private float fadeSpeed = 1f; //漸隐漸現的速率
private bool sceneStarting = true; //場景是否開始
private RawImage rawImage;
void Start()
{
RectTransform rectTransform = this.GetComponent<RectTransform>();
//使背景滿屏
rectTransform.sizeDelta = new Vector2(Screen.width, Screen.height);
rawImage = this.GetComponent<RawImage>();
rawImage.uvRect = new Rect(0, 0, Screen.width, Screen.height);
rawImage.enabled = true;
}
void Update()
{
if (sceneStarting)
{
StartScene();
}
}
/// <summary>
/// 漸隐效果
/// </summary>
private void FadeToClear()
{
rawImage.color = Color.Lerp(rawImage.color, Color.clear, fadeSpeed * Time.deltaTime);
}
/// <summary>
/// 漸現效果
/// </summary>
private void FadeToBlack()
{
rawImage.color = Color.Lerp(rawImage.color, Color.black, fadeSpeed * Time.deltaTime);
}
/// <summary>
/// 場景開始時漸隐
/// </summary>
private void StartScene()
{
FadeToClear();
if (rawImage.color.a <= 0.05f)
{
rawImage.color = Color.clear;
rawImage.enabled = false;
sceneStarting = false;
}
}
/// <summary>
/// 場景結束時漸現
/// </summary>
public void EndScene()
{
rawImage.enabled = true;
FadeToBlack();
if (rawImage.color.a >= 0.95f)
{
SceneManager.LoadScene("Scene_1");
}
}
}
FPS_Camera.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class FPS_Camera : MonoBehaviour
{
public Vector2 mouseLookSensitivity = new Vector2(3, 3); //滑鼠靈敏度
public Vector2 rotationXLimit = new Vector2(-87, 87); //上下旋轉限制
public Vector2 rotationYLimit = new Vector2(-360, 360); //左右旋轉限制
public Vector3 positionOffset = new Vector3(0, 2, -0.2f); //位置偏移
private Vector2 currentMouseLook = Vector2.zero; //目前滑鼠
private float x_Angle = 0; //X軸旋轉角度
private float y_Angle = 0; //Y軸旋轉角度
private FPS_PlayerParameters parameters; //
private Transform mTransform; //玩家執行個體
private void Start()
{
parameters = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<FPS_PlayerParameters>();
mTransform = transform;
mTransform.localPosition = positionOffset;
}
private void Update()
{
InputUpdate();
LateUpdate();
}
private void LateUpdate()
{
//左右旋轉
Quaternion xQuaternion = Quaternion.AngleAxis(y_Angle, Vector3.up);
//上下旋轉
Quaternion yQuaternion = Quaternion.AngleAxis(0, Vector3.left);
//角色不旋轉
mTransform.parent.rotation = xQuaternion * yQuaternion;
yQuaternion = Quaternion.AngleAxis(x_Angle, Vector3.left);
//錄影機旋轉
mTransform.rotation = xQuaternion * yQuaternion;
}
/// <summary>
/// 更新使用者輸入處理
/// </summary>
private void InputUpdate()
{
//如果滑鼠還沒有動作
if(parameters.inputSmoothLook == Vector2.zero)
{
return;
}
GetMouseLook();
//繞Y軸旋轉實際上是水準旋轉
y_Angle += currentMouseLook.x;
//繞X軸旋轉實際上是豎直旋轉
x_Angle += currentMouseLook.y;
//避免Y軸超出旋轉限制
y_Angle = y_Angle < -360 ? y_Angle + 360 : y_Angle;
y_Angle = y_Angle > 360 ? y_Angle - 360 : y_Angle;
y_Angle = Mathf.Clamp(y_Angle, rotationYLimit.x, rotationYLimit.y);
//避免X軸超出旋轉限制
x_Angle = x_Angle < -360 ? x_Angle + 360 : x_Angle;
x_Angle = x_Angle > 360 ? x_Angle - 360 : x_Angle;
x_Angle = Mathf.Clamp(x_Angle, rotationXLimit.y, rotationXLimit.x);
}
/// <summary>
/// 目前視角跟随滑鼠移動
/// </summary>
private void GetMouseLook()
{
currentMouseLook.x = parameters.inputSmoothLook.x;
currentMouseLook.y = parameters.inputSmoothLook.y;
//視角旋轉加入靈敏度
currentMouseLook.x *= mouseLookSensitivity.x;
currentMouseLook.y *= mouseLookSensitivity.y;
}
}
FPS_CrossHair.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FPS_CrossHair : MonoBehaviour
{
public float length; //長度
public float width; //寬度
public float distance; //間距
public Texture2D crossHairTexture; //2D紋理
private GUIStyle lineStyle; //樣式
private Texture texture; //紋理
private void Start()
{
lineStyle = new GUIStyle();
lineStyle.normal.background = crossHairTexture;
}
private void OnGUI()
{
//十字坐标左邊矩形
GUI.Box(new Rect(Screen.width / 2 - distance / 2 - length, Screen.height / 2 - width / 2, length, width), texture, lineStyle);
//十字坐标右邊矩形
GUI.Box(new Rect(Screen.width / 2 + distance / 2, Screen.height / 2 - width / 2, length, width), texture, lineStyle);
//十字坐标上邊矩形
GUI.Box(new Rect(Screen.width / 2 - width / 2, Screen.height / 2 - distance / 2 - length, width, length), texture, lineStyle);
//十字坐标下邊矩形
GUI.Box(new Rect(Screen.width / 2 - width / 2, Screen.height / 2 + distance / 2, width, length), texture, lineStyle);
}
}
一個堅持學習,堅持成長,堅持分享的人,即使再不聰明,也一定會成為優秀的人!
整理不易,如果看完覺得有所收獲的話,記得一鍵三連哦,謝謝!