// Update is called once per frame void Update() { if (Input.GetKeyUp(KeyCode.Escape)) { if (EscCanvas.activeSelf) { EscCanvas.SetActive(false); Time.timeScale = 1; Cursor.visible = false; } else { EscCanvas.SetActive(true); Time.timeScale = 0; Cursor.visible = true; } } if (isDead) { return; } /*设置视线关注点 */ Ray sightRay = Camera.main.ScreenPointToRay(new Vector3(Camera.main.pixelWidth / 2, Camera.main.pixelHeight / 2, 0f)); RaycastHit sightHit; if (Physics.Raycast(sightRay, out sightHit)) { sightTargetPoint = sightHit.point; } /*更新血量 */ if (health != playerController.health) { health = playerController.health; healthTransform.localScale = new Vector3(health / MaxHealth, 1f, 1f); } if (health == 0) { //死亡处理 StopCoroutine("StartTime"); OverGame(); return; } /*准星相关处理 */ float bias = sightController.GetBias(); //射击加偏置 Ray ray = Camera.main.ScreenPointToRay(new Vector3(Camera.main.pixelWidth / 2 + UnityEngine.Random.Range(-bias, bias), Camera.main.pixelHeight / 2 + UnityEngine.Random.Range(-bias, bias), 0f)); bool isRaycast = Physics.Raycast(ray, out hit); //标记准星是否有目标点 if (isRaycast) { //若为武器且在捡取范围内 if (hit.collider.gameObject.tag.Equals("Weapon") && (hit.point - transform.position).magnitude <= MaxPickDistance) { prompt.gameObject.SetActive(true); //换枪 if (Input.GetKey("f")) { knife.SetActive(false); //若有武器则显示丢弃武器动画 if (gun != null && gun.GetComponent <WeaponController>().category != WeaponCategory.Knife) { DropGun(); } gun = hit.collider.gameObject; weaponNow = gun; weaponController = weaponNow.GetComponent <WeaponController>(); //去掉碰撞与重力 Collider collider = gun.GetComponent <MeshCollider>(); if (collider != null) { Destroy(collider); } Rigidbody rigidbody = gun.GetComponent <Rigidbody>(); if (rigidbody != null) { Destroy(rigidbody); } //正确显示动画 switch (weaponController.category) { case WeaponCategory.Handgun: playerController.ToHandgun(gun); break; case WeaponCategory.Heavy: playerController.ToHeavy(gun); break; case WeaponCategory.Infantry: playerController.ToInfantry(gun); break; } //正确显示UI SetBulletUI(); SetWeaponInfoUI(); } } else { prompt.gameObject.SetActive(false); } } /*以下所有处理同一时间只能触发一个 */ //丢弃主武器 if (Input.GetKey("g") && gun != null) { DropGun(); //切换为刀 knife.SetActive(true); playerController.ToKnife(knife); weaponNow = knife; weaponController = weaponNow.GetComponent <WeaponController>(); //正确显示UI SetBulletUI(); SetWeaponInfoUI(); } //攻击,不能同时用两种攻击方式 else if (Input.GetAxisRaw("Fire1") == 1 && ((weaponController.shootMode == ShootMode.Single && playerController.Attack1()) /*单发模式 */ || (weaponController.shootMode == ShootMode.Auto && playerController.Attack2()) /*连发模式 */)) { // Debug.Log((hit.point - transform.position).magnitude); //判断是否能击中 if (weaponController.Shoot() && isRaycast && (hit.point - transform.position).magnitude <= weaponController.maxAttackDis) { shootAngle += shootAngleChangeSpeed; //后坐力 //除刀外改变准星准度 if (weaponController.category != WeaponCategory.Knife) { sightController.Shoot(); } //改变弹药显示 SetBulletUI(); //若击中环境,刀子无击中效果 if (hit.collider.gameObject.layer == 9 && weaponController.category != WeaponCategory.Knife) { GameObject shootSmoke = Instantiate(shootSmokePrefab, hit.point, Quaternion.identity); shootSmoke.transform.localRotation.SetLookRotation(hit.normal); //获取击中物体的颜色 Color color = hit.collider.gameObject.GetComponent <MeshRenderer>().material.color; ParticleSystem particleSystem = shootSmoke.GetComponent <ParticleSystem>(); //设置起始颜色 ParticleSystem.MainModule main = particleSystem.main; main.startColor = color; //设置颜色渐变 ParticleSystem.ColorOverLifetimeModule col = particleSystem.colorOverLifetime; col.enabled = true; Gradient grad = new Gradient(); grad.SetKeys(new GradientColorKey[] { new GradientColorKey(color, 0.4f), new GradientColorKey(color, 1.0f) }, new GradientAlphaKey[] { new GradientAlphaKey(1.0f, 0.0f), new GradientAlphaKey(0.0f, 1.0f) }); col.color = grad; GameObject.Destroy(shootSmoke, 0.5f); //设置0.2s后销毁烟雾效果 } //若击中人 else if (hit.collider.gameObject.layer == 11) { HumanAnimController rivalController = hit.collider.gameObject.GetComponent <HumanAnimController>(); rivalController.Damaged(weaponController.damage, hit.point); //若敌人被打死则加分数 if (rivalController.health <= 0 && !hit.collider.gameObject.GetComponent <RivalController>().isDead) { scoreText.text = (int.Parse(scoreText.text) + 1).ToString(); } //击中显示 StopCoroutine("ShowHitUI"); StartCoroutine("ShowHitUI"); } } } else if (Input.GetAxisRaw("Fire2") == 1 && weaponController.category == WeaponCategory.Knife && playerController.Attack2()) { // (hit.collider.transform.position - transform.position).magnitude < weaponController.maxAttackDis || if (isRaycast && (hit.point - transform.position).magnitude <= weaponController.maxAttackDis) { //若击中人 if (hit.collider.gameObject.layer == 11) { hit.collider.gameObject.GetComponent <HumanAnimController>().Damaged(weaponController.damage, hit.point); //击中显示 StopCoroutine("ShowHitUI"); StartCoroutine("ShowHitUI"); } } } else if (Input.GetKey("r") && weaponController.category != WeaponCategory.Knife && playerController.Reload()) { } else if (Input.GetKey("y") && weaponController.ChangeShootMode()) { SetWeaponInfoUI(); } //切换武器 else if (Input.GetKeyUp("q") && gun != null) { if (weaponNow == gun) { gun.SetActive(false); knife.SetActive(true); weaponNow = knife; weaponController = weaponNow.GetComponent <WeaponController>(); playerController.ToKnife(knife); } else { knife.SetActive(false); gun.SetActive(true); weaponNow = gun; weaponController = weaponNow.GetComponent <WeaponController>(); switch (weaponController.category) { case WeaponCategory.Handgun: playerController.ToHandgun(gun); break; case WeaponCategory.Heavy: playerController.ToHeavy(gun); break; case WeaponCategory.Infantry: playerController.ToInfantry(gun); break; } } //正确显示UI SetBulletUI(); SetWeaponInfoUI(); } if (bulletSumLast != weaponController.bulletSum) { if (weaponController.category == WeaponCategory.Knife) { weaponBulletSum.text = ""; } else { weaponBulletSum.text = "剩余弹药:" + weaponController.bulletSum; } bulletSumLast = weaponController.bulletSum; } }
// Update is called once per frame void Update() { //死亡则慢慢下沉消失 if (humanController.health <= 0 && !isDead) { if (dieCallbacks != null) { dieCallbacks(gameObject); //死亡回调 } isDead = true; Destroy(GetComponent <CharacterController>()); Destroy(GetComponent <Rigidbody>()); Destroy(GetComponent <SphereCollider>()); StopAllCoroutines(); StartCoroutine(Die()); return; } if (humanController.health != humanController.MaxHealth && humanController.health > 0 && !isTrace && !isShoot) { isTrace = true; isPatrol = false; isShoot = false; } if (isInRotate) { return; } // Debug.Log("Next"); if (!isDead) { //移动 if (isPatrol) { // Debug.Log("WALK"); // nav.enabled = false; // StopCoroutine("Trace"); humanController.Idle(); if (Vector3.Angle(patrolForward, transform.forward) > 5.0f) //判断是否需要转向 { // Debug.Log("ChangeTo:"+patrolForward + transform.forward + (patrolForward == transform.forward) +" "+); // StopCoroutine("RotateByFrame"); RotateTowards(patrolForward); return; } humanController.Walk(walkSpeed, 0f); } else if (isTrace) //追踪 { if (playerLastPosition == Vector3.zero || (Global.player.transform.position - playerLastPosition).magnitude > 3f) { /*向服务器发送路径计算请求 */ byteBuilder.Add(System.BitConverter.GetBytes(Network.RIVAL_REQUEST)); byteBuilder.Add(System.BitConverter.GetBytes(id)); byteBuilder.Add(System.BitConverter.GetBytes(Network.SEARCH_ROAD)); byteBuilder.Add(System.BitConverter.GetBytes((int)(transform.position.x + 0.5))); byteBuilder.Add(System.BitConverter.GetBytes((int)(transform.position.z + 0.5))); byteBuilder.Add(System.BitConverter.GetBytes((int)(Global.player.transform.position.x + 0.5))); byteBuilder.Add(System.BitConverter.GetBytes((int)(Global.player.transform.position.z + 0.5))); Global.network.Send(byteBuilder.GetByes()); byteBuilder.Clear(); // nav.enabled = true; // nav.SetDestination(Global.player.transform.position); // humanController.Run(0, 0); playerLastPosition = Global.player.transform.position; } } else if (vertical != 0 || horizontal != 0) { humanController.Run(vertical * runSpeed, horizontal * runSpeed); // nav.enabled = false; StopCoroutine("Trace"); humanController.Idle(); } else { // humanController.Idle(); // nav.enabled = false; // StopCoroutine("Trace"); StopAllCoroutines(); humanController.Idle(); } //设置视线 Ray tar = new Ray(transform.position + shootRectify, transform.forward); if (Physics.Raycast(tar, out sightHit)) { lookAtPoint = sightHit.point; } //检测是否玩家在视线内 Vector3 targetV = Global.player.transform.position - transform.position; // Debug.Log(Vector3.Angle(transform.forward, targetV)); if (Vector3.Angle(transform.forward, targetV) <= sightAngle) { Ray detect = new Ray(transform.position + shootRectify, targetV); RaycastHit h; if (Physics.Raycast(detect, out h)) { // Debug.Log("Hit:"+h.collider.gameObject.layer+" "+h.collider.name); // Debug.DrawRay(transform.position, targetV, Color.black); if (h.collider.gameObject.layer == 11) { // Debug.Log("Human"); float dis = (transform.position - Global.player.transform.position).magnitude; if (!isTrace && !isShoot) //若在巡逻状态 { Debug.Log("In Sight"); //在视线内 byteBuilder.Add(System.BitConverter.GetBytes(Network.RIVAL_REQUEST)); byteBuilder.Add(System.BitConverter.GetBytes(id)); byteBuilder.Add(System.BitConverter.GetBytes(Network.PLAYER_IN_SIGHT)); Global.network.Send(byteBuilder.GetByes()); byteBuilder.Clear(); } else if (!isShoot && dis < weaponController.maxAttackDis) //若不在射击状态 { byteBuilder.Add(System.BitConverter.GetBytes(Network.RIVAL_REQUEST)); byteBuilder.Add(System.BitConverter.GetBytes(id)); byteBuilder.Add(System.BitConverter.GetBytes(Network.PLAYER_IN_SHOOT)); Global.network.Send(byteBuilder.GetByes()); byteBuilder.Clear(); } else if (isShoot && dis > weaponController.maxAttackDis) //若在射击状态但不在攻击距离内 { byteBuilder.Add(System.BitConverter.GetBytes(Network.RIVAL_REQUEST)); byteBuilder.Add(System.BitConverter.GetBytes(id)); byteBuilder.Add(System.BitConverter.GetBytes(Network.PLAYER_OUT_SHOOT)); Global.network.Send(byteBuilder.GetByes()); byteBuilder.Clear(); } } else if (isShoot) //出视线外 { byteBuilder.Add(System.BitConverter.GetBytes(Network.RIVAL_REQUEST)); byteBuilder.Add(System.BitConverter.GetBytes(id)); byteBuilder.Add(System.BitConverter.GetBytes(Network.PLAYER_OUT_SHOOT)); Global.network.Send(byteBuilder.GetByes()); byteBuilder.Clear(); } } } //shoot if (isShoot) { Debug.Log("SHOOT"); //若无子弹则换弹 if (weaponController.bulletNumNow <= 0) { weaponController.bulletSum = weaponController.bulletMaxHold + 100; //设置子弹无限 humanController.Reload(); } else { transform.LookAt(Global.player.transform, transform.up); if (humanController.Attack1() && weaponController.Shoot()) { Ray ray = new Ray(transform.position + shootRectify, transform.forward + transform.right * Random.Range(-shootBias, shootBias) + transform.up * Random.Range(-shootBias, shootBias)); if (Physics.Raycast(ray, out hit)) { //若击中环境,刀子无击中效果 if (hit.collider.gameObject.layer == 9 && weaponController.category != WeaponCategory.Knife) { GameObject shootSmoke = Instantiate(shootSmokePrefab, hit.point, Quaternion.identity); shootSmoke.transform.localRotation.SetLookRotation(hit.normal); //获取击中物体的颜色 Color color = hit.collider.gameObject.GetComponent <MeshRenderer>().material.color; ParticleSystem particleSystem = shootSmoke.GetComponent <ParticleSystem>(); //设置起始颜色 ParticleSystem.MainModule main = particleSystem.main; main.startColor = color; //设置颜色渐变 ParticleSystem.ColorOverLifetimeModule col = particleSystem.colorOverLifetime; col.enabled = true; Gradient grad = new Gradient(); grad.SetKeys(new GradientColorKey[] { new GradientColorKey(color, 0.4f), new GradientColorKey(color, 1.0f) }, new GradientAlphaKey[] { new GradientAlphaKey(1.0f, 0.0f), new GradientAlphaKey(0.0f, 1.0f) }); col.color = grad; GameObject.Destroy(shootSmoke, 0.5f); //设置0.2s后销毁烟雾效果 } //若击中人 else if (hit.collider.gameObject.layer == 11) { hit.collider.gameObject.GetComponent <HumanAnimController>().Damaged(weaponController.damage, hit.point); } } } } } } }