失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 【Unity学习笔记】第一人称射击游戏

【Unity学习笔记】第一人称射击游戏

时间:2021-12-04 00:18:05

相关推荐

【Unity学习笔记】第一人称射击游戏

1、新建一个地面Plan。

2、搭建好Player模型。把枪的模型拖入,调整好角度。由于是第一人称游戏。把camera也拖入Player下。

3、编写playerMove脚本,实现asdw控制人物的前后左右移动 空格键跳跃的功能

知识点1:由于是3D第一人称游戏,玩家的移动应以自身坐标轴为准,玩家移动脚本编写为

//人物的位移 以自身坐标为准transform.Translate(new Vector3(x, 0, y)*speed*Time.deltaTime,Space.Self);

知识点2:玩家地面监测:设置玩家不能在空中跳跃,需要借助射线检测玩家是否碰及物体

bool IsGround(){//打印射线以便观察Debug.DrawRay(transform.position, Vector3.down,Color.red,1f);//以当前物体的中心点为起点向下发射一条长度为distance的射线,射线检测是否触碰到碰撞盒 返回true/false//射线检测 以哪一位置为中心 向哪个方向发射 向下的距离 Vector3.down=new Vector(0,-1,0);return Physics.Raycast(transform.position,Vector3.down,distance);}

using System.Collections;using System.Collections.Generic;using UnityEngine;public class _PlayerMove : MonoBehaviour{//asdw控制人物的前后左右移动 空格键跳跃//改变人物的transformpublic float speed = 5f;public float distance = 0.2f;void Update(){float x = Input.GetAxis("Horizontal");float y = Input.GetAxis("Vertical");//人物的位移 以自身坐标为准transform.Translate(new Vector3(x, 0, y)*speed*Time.deltaTime,Space.Self);if (Input.GetButtonDown("Jump")){// Debug.Log(IsGround());//获取刚体,给物体添加一个向上的力if (IsGround()){GetComponent<Rigidbody>().velocity = transform.up * speed;}}}bool IsGround(){//打印射线以便观察Debug.DrawRay(transform.position, Vector3.down,Color.red,1f);//以当前物体的中心点为起点向下发射一条长度为distance的射线,射线检测是否触碰到碰撞盒 返回true/false//射线检测 以哪一位置为中心 向哪个方向发射 向下的距离 Vector3.down=new Vector(0,-1,0);return Physics.Raycast(transform.position,Vector3.down,distance);}}

4、编写PlayerRotation脚本 用鼠标控制玩家旋转

using System.Collections;using System.Collections.Generic;using UnityEngine;public class _PlayerRotation : MonoBehaviour{//鼠标的x轴移动方向及速度float x;float y;public float Speed = 2f;public GameObject MainCarm;// Start is called before the first frame updatevoid Start(){x = MainCarm.transform.eulerAngles.x;}// Update is called once per framevoid Update(){//若按下鼠标右键if(Input.GetMouseButton(1)){//人物Y轴变化量y = Input.GetAxis("Mouse X")*Speed;//物体以Y轴为中心进行旋转 //Quaternion.Euler(0, y, 0) 把欧拉角转化成对应的四元数transform.localRotation = transform.localRotation * Quaternion.Euler(0, y, 0);//摄像机X轴数值x -= Input.GetAxis("Mouse Y") * Speed;//限定范围x = Mathf.Clamp(x,-60, 45);//改变摄像机自身的X轴数值(欧拉角)MainCarm.transform.localEulerAngles = new Vector3(x, 0, 0);//MainCarm.transform.localEulerAngles = new Vector3(x, 0, MainCarm.transform.eulerAngles.z);}}}

发射子弹,装填子弹。当装填子弹时,子弹不能发射

void Update(){stateInfo = anim.GetCurrentAnimatorStateInfo(0);gunAnimStart();if (gunType==0)text.text = "子弹数量:" +"无穷";elsetext.text = "子弹数量:" + buttleNumber;//切换枪支if (Input.GetKeyDown(KeyCode.Q)){gunType++;gunType=gunType%2;playerGunState.Close();playerGunState.isbool = true;if (gunType == 0){object98k.SetActive(true);objectM24.SetActive(false);anim = object98k.GetComponent<Animator>();stateInfo = anim.GetCurrentAnimatorStateInfo(0);buttleNumber = 10;//初始化}else{object98k.SetActive(false);objectM24.SetActive(true);anim = objectM24.GetComponent<Animator>();stateInfo = anim.GetCurrentAnimatorStateInfo(0);}}if (stateInfo.fullPathHash == Animator.StringToHash("Base Layer.Bolt")){anim.SetInteger("shoot", -1);//}if (stateInfo.fullPathHash == Animator.StringToHash("Base Layer.Idle")) {// Debug.Log("11111");if (Input.GetMouseButtonDown(0)){gunfire();if(buttleNumber>0)buttleNumber--;anim.SetInteger("shoot", 1);//切换到发射}}playermove();}public void playermove(){float x = Input.GetAxis("Horizontal");float y = Input.GetAxis("Vertical");//人物的位移 以自身坐标为准transform.Translate(new Vector3(x, 0, y) * speed * Time.deltaTime, Space.Self);if (Input.GetButtonDown("Jump")){Debug.Log(IsGround());//获取刚体,给物体添加一个向上的力if (IsGround()){GetComponent<Rigidbody>().velocity = transform.up * speed;}}}public void gunfire(){GameObject bullets = Resources.Load<GameObject>("bullet");GameObject bullet1 = Instantiate(bullets, GunPosition.position, GunPosition.rotation);Vector3 hitPoint;//从屏幕中央位置发射一条射线Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));//射线是否触碰到物体if(Physics.Raycast(ray,out raycastHit)){//Debug.Log()hitPoint = raycastHit.point;bullet1.transform.LookAt(hitPoint);//将子弹看向发射的点//子弹的飞行路径Debug.DrawLine(GunPosition.position, hitPoint, Color.red, 3f);//camera的 Debug.DrawLine打印的是一段射线, Debug.DrawRay是朝某个位置发射射线Debug.DrawLine(Camera.main.transform.position, hitPoint, Color.yellow, 3f);}}

如果觉得《【Unity学习笔记】第一人称射击游戏》对你有帮助,请点赞、收藏,并留下你的观点哦!

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