当前位置:   article > 正文

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

unity fps游戏

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

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

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

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

游戏展示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

资源

链接:https://pan.baidu.com/s/15s8KSN_4JPAvD_fA8OIiCg
想要资源的同学关注公众号输入【FPS】即可获取密码下载资源,这是小编做的公众号,里面有各种外卖优惠券,有点外卖需求的话各位同学可以支持下!
在这里插入图片描述

在这里插入图片描述

代码

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);
    }
}
  • 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

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");
        }
    }
}

  • 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
  • 75
  • 76
  • 77
  • 78

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;
    }
}

  • 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
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85

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);
    }
}

  • 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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/104172
推荐阅读
相关标签
  

闽ICP备14008679号