// Start is called before the first frame update void Start() { //初始化视线 Ray tar = new Ray(transform.position + shootRectify, transform.forward); if (Physics.Raycast(tar, out sightHit)) { lookAtPoint = sightHit.point; } //初始化动作 switch (weaponController.category) { case WeaponCategory.Handgun: humanController.ToHandgun(weapon); break; case WeaponCategory.Heavy: humanController.ToHeavy(weapon); break; case WeaponCategory.Infantry: humanController.ToInfantry(weapon); break; case WeaponCategory.Knife: humanController.ToKnife(weapon); break; } SetPatrol(); }
// 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; } }