private void FixedUpdate() { //移动视角 mouseX = Input.GetAxis("Mouse X"); if (mouseX != 0f) { if (!isDead) { transform.Rotate(0, mouseX * viewHorizontalSpeed, 0, Space.World); } else { Camera.main.transform.RotateAround(transform.position, Vector3.up, mouseX * viewHorizontalSpeed); } } if (isDead) { return; } mouseY = Input.GetAxis("Mouse Y"); if (mouseY != 0f || shootAngle != 0f) { float angle = mouseY * viewVerticalSpeed + shootAngle; float targetAngle = viewAngle + angle; if (targetAngle > 0 && targetAngle > viewMaxAngle) { angle = viewMaxAngle - viewAngle; viewAngle = viewMaxAngle; } else if (targetAngle < 0 && targetAngle < -viewMaxAngle) { angle = -viewMaxAngle - viewAngle; viewAngle = -viewMaxAngle; } else { viewAngle = targetAngle; } Camera.main.transform.RotateAround(transform.position, transform.right, -angle); shootAngle = 0f; //移动摄像头后清空射击后坐力偏移 } //蹲下 if (Input.GetKey("left ctrl")) { if (!isCrouching) { playerController.SetCrouching(); isCrouching = true; } } else { if (isCrouching) { isCrouching = false; playerController.SetStanding(); } } //移动与奔跑 vertical = Input.GetAxisRaw("Vertical"); horizontal = Input.GetAxisRaw("Horizontal"); if (vertical != 0f || horizontal != 0f) { //限制蹲下时不能跑 if (Input.GetKey("left shift") && !isCrouching) { playerController.Run(vertical * runSpeed, horizontal * runSpeed); } else { playerController.Walk(vertical * walkSpeed, horizontal * walkSpeed); } } else { playerController.Idle(); } //跳 if (Input.GetAxisRaw("Jump") == 1) { playerController.Jump(); } }
// 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); } } } } } } }