1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Unity3D FPS Game:第一人称射击游戏(一)

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

时间:2023-03-30 00:51:11

相关推荐

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

耗时一周制作的第一人称射击游戏,希望能帮助到大家!

由于代码较多,分为三篇展示,感兴趣的朋友们可以点击查看!

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

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

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

文章目录

游戏展示资源代码AnimatorSetup.csFadeInOut.csFPS_Camera.csFPS_CrossHair.cs

游戏展示

资源

链接:/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);}}

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。