Unity3D FPS Game:第一人称射击游戏(一)

耗时一周制作的第一人称射击游戏,希望能帮助到大家!
由于代码较多,分为三篇展示,感兴趣的朋友们可以点击查看!

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
想要资源的同学关注公众号输入【FPS】即可获取密码下载资源,这是小编做的公众号,里面有各种外卖优惠券,有点外卖需求的话各位同学可以支持下!
Unity3D FPS Game:第一人称射击游戏(一)_第1张图片

Unity3D FPS Game:第一人称射击游戏(一)_第2张图片

代码

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;

/// 
/// 渐隐渐现效果
/// 
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();
        }
    }

    /// 
    /// 渐隐效果
    /// 
    private void FadeToClear()
    {
        rawImage.color = Color.Lerp(rawImage.color, Color.clear, fadeSpeed * Time.deltaTime);
    }

    /// 
    /// 渐现效果
    /// 
    private void FadeToBlack()
    {
        rawImage.color = Color.Lerp(rawImage.color, Color.black, fadeSpeed * Time.deltaTime);
    }

    /// 
    /// 场景开始时渐隐
    /// 
    private void StartScene()
    {
        FadeToClear();
        if (rawImage.color.a <= 0.05f)
        {
            rawImage.color = Color.clear;
            rawImage.enabled = false;
            sceneStarting = false;
        }
    }

    /// 
    /// 场景结束时渐现
    /// 
    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;
    }

    /// 
    /// 更新用户输入处理
    /// 
    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);
    }

    /// 
    /// 当前视角跟随鼠标移动
    /// 
    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);
    }
}

一个坚持学习,坚持成长,坚持分享的人,即使再不聪明,也一定会成为优秀的人!

整理不易,如果看完觉得有所收获的话,记得一键三连哦,谢谢!

你可能感兴趣的:(Unity3D,游戏开发,游戏,unity3d,3d游戏,游戏引擎)