- 通过键盘控制玩家的移动
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { CharacterController cc; void Start() { cc = GetComponent<CharacterController>(); } void Move() { float x = Input.GetAxis(StaticGameResources.Horizontal); float z = Input.GetAxis(StaticGameResources.Vertical); //Vector3 v = new Vector3(x,0,z);//若玩家旋转之后仍以世界坐标为方向轴,会出现方向错乱,水平轴和垂直轴的输入达不到预期效果,即不能正确的向前向后向左向右; Vector3 dir = transform.forward * z + transform.right * x;//第一人称 if (Mathf.Abs(x) > 0 || Mathf.Abs(z) > 0) { if (!audios.isPlaying)//防止上一段音频还没播放完毕,下一段音频的播放产生重叠干扰; { audios.Play(); } cc.SimpleMove(dir * Time.deltaTime * 100); } } }
2.按下空格键玩家跳起并落下
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { CharacterController cc; void Start() { cc = GetComponent<CharacterController>(); } void Jump() { nextTimer += Time.deltaTime; if (Input.GetKeyDown(KeyCode.Space) && nextTimer - lastTimer >= 0.5f)//由于角色控制器的SimpleMove自带刚体,但只有前后左右移动才会调用SimpleMove才拥有重力,所以玩家角色的落下采取了延时下落的方法,并将延时下落的时间作为两次起跳的间隔时间,防止玩家角色梯云纵; { //audios.clip = jumpClip; //audios.Play();//同一个音频组件频繁切换音频,每段饮品播放完毕需要切换至下一个音频,太过繁琐,逻辑上需要过多的思考,不建议采用; AudioSource.PlayClipAtPoint(jumpClip, Camera.main.transform.position, 1); lastTimer = nextTimer;//当玩家起跳的的一瞬间将值赋给lastTimer(上一次跳跃时间); transform.position = new Vector3(transform.position.x, 19.57f, transform.position.z); Invoke("Down", 0.5f); } } void Down() { transform.transform.position = (new Vector3(transform.position.x, 18.57f, transform.position.z)); } }
3.点击鼠标左键开枪(射线从枪口射出),有射击特效,射击到物体上面有弹孔克隆,射击距离在100米范围内
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { CharacterController cc; void Start() { cc = GetComponent<CharacterController>(); } void Fire() { if (Input.GetMouseButtonDown(0)) { ps = PlayerState.Fire; //audios.clip = shootClip; //audios.Play();//多音频切换太过繁琐,开火完成需要切回脚步声/跳跃声; AudioSource.PlayClipAtPoint(shootClip,Camera.main.transform.position,1); Ray ray = new Ray(firePos.position, firePos.forward);//枪支上的空物体位置 RaycastHit hit; Physics.Raycast(ray, out hit,100); Debug.DrawLine(ray.origin, hit.point); GameObject go = Instantiate(shootVFX, firePos.position, transform.rotation);//克隆开火特效; Destroy(go, 0.1f); GameObject go1 = Instantiate(hole, hit.point, Quaternion.identity);//克隆弹孔 go1.transform.LookAt(hit.point - hit.normal);//与法线垂直的向量 go1.transform.Translate(Vector3.back * 0.03f); Destroy(go1, 1f); //if (hit.collider.CompareTag(StaticGameResources.)) //{ //} } } }
4.玩家先拿到钥匙才能开门,玩家如果没有拿到钥匙,碰到激光墙,有警告声音响起,并使玩家生命值减少,如果玩家拿到钥匙,碰到激光墙,墙体自动提升,10秒后自动落下
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { CharacterController cc; void Start() { cc = GetComponent<CharacterController>(); } void AirWallUp() { airWall.transform.position = new Vector3(transform.position.x,transform.position.y+10,transform.position.z); Invoke("AirWallDown", 10); } void AirWallDown() { airWall.transform.position =new Vector3(transform.position.x, transform.position.y - 10, transform.position.z); } private void OnControllerColliderHit(ControllerColliderHit hit) { if (hit.collider.CompareTag(StaticGameResources.keyCardTag))//当角色控制器碰撞到门卡,将门卡作为玩家的子对象,并且对门卡进行隐藏,伪装一个将物体收入背包的操作; { hit.gameObject.SetActive(false);//隐藏后不一定能判断到;解决思路:可以在脚本中创建空物体并对其添加和门卡一样的标签; //经过实测,物体设为隐藏状态,同样可以查找到; hit.collider.gameObject.transform.SetParent(transform); } if (hit.collider.CompareTag(StaticGameResources.airWallTag))//当玩家触碰到空气墙时,创建一个布尔值,对玩家的子物体进行遍历,若在玩家的子物体中发现门卡则结果为真,激光门自动抬上,10s后复位; { bool b = false; for (int i = 0; i < transform.childCount; i++) { if (transform.GetChild(i).CompareTag(StaticGameResources.keyCardTag)) { b = true; break; } } if (b) { AirWallUp(); } else { //玩家血量--; if (!cameraAudios.isPlaying)//通过标签获取到相机上挂在的警报声; { cameraAudios.Play(); } } } } }
CS大战1
最新推荐文章于 2024-07-17 16:41:35 发布