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

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

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

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

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

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

  • 43
    点赞
  • 166
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拿头来坚持

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值