using System.Collections.Generic; using UnityEngine; public class Arrow : MonoBehaviour { private Rigidbody newRigidbody; public float flyTime = 0; public bool isHit = false; public ArmBow armBow; public static float speed = 4; void Start() { // CrossHair.ins.gameObject.SetActive(false); newRigidbody = this.gameObject.AddComponent(); newRigidbody.velocity = this.transform.forward * speed; newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic; this.transform.Find("Camera").gameObject.SetActive(true); this.activeEffectCyclone(true); this.activeEffectTrail(true); } Vector3 position; Quaternion rotation; void FixedUpdate() { if (newRigidbody != null) { transform.forward = newRigidbody.velocity.normalized; } if (!isHit && flyTime >= 0) { flyTime += Time.deltaTime; this.position = this.transform.position; this.rotation = this.transform.rotation; if (flyTime > 20.0) { Destroy(gameObject); GameMgr.ins.hitTarget(0); AudioMgr.ins.PlayCheer(false); nextShoot(); } this.UpdateRotate(); } this.UpdateShake(); this.updateMoveCamera(); if (!this.isHit) { this.updateEffectCyclone(); } } public void Hit() { gameObject.GetComponent().enabled = false; if (newRigidbody != null) { newRigidbody.useGravity = false; newRigidbody.velocity = Vector3.zero; Destroy(newRigidbody); newRigidbody = null; } isHit = true; // this.Shake(); this.activeEffectCyclone(false); this.activeEffectBomb(true); this.activeEffectTrail(false); } public Vector3 getHeadPosition() { return this.transform.Find("Head").position; } /**箭矢旋转 */ private Vector3 rotateV3 = new Vector3(0, 0, 1400); private void UpdateRotate() { this.transform.Find("Head").Rotate(rotateV3 * Time.deltaTime, Space.Self); } /**箭矢震动 */ private bool shaking = false; private float shakeTime = 0; private float shakeTimeMax = 0.5f; private float shakeRange = 5; private int shakeDirection = 1; private Vector3 shakeAngles = new Vector3(); private void Shake() { this.shaking = true; this.shakeTime = 0; this.shakeDirection = 1; } private void UpdateShake() { if (!this.shaking) { return; } Transform transform = this.transform.Find("Head").transform; float shakeRangeNow = (1 - this.shakeTime / this.shakeTimeMax) * this.shakeRange; if (shakeRangeNow <= 0) {//震动结束 this.shaking = false; this.shakeAngles.x = 0; } else { this.shakeAngles.x = -this.shakeDirection * shakeRangeNow; this.shakeDirection *= -1; } transform.localEulerAngles = this.shakeAngles; this.shakeTime += Time.deltaTime; } /**相机移动 */ private Vector3 cameraPosition = new Vector3(); private bool cameraMoveFinish = false; //相机的位置xyz变化 【变化量,最小值,最大值】 private float[] cpcs = { //运动中 0, 0, 0, 1.5f, 0, 0.25f, -7, -1.5f, 0, //击中后 -3.5f, -1.2f, 0, -3.5f, 0, 0.5f, 7, -1.5f, -1.5f }; private void updateMoveCamera() { if (cameraMoveFinish) { return; } Transform cameraT = this.transform.Find("Camera"); cameraPosition.x = cameraT.localPosition.x; cameraPosition.y = cameraT.localPosition.y; cameraPosition.z = cameraT.localPosition.z; if (this.isHit) { cameraPosition.x = Mathf.Clamp(cameraPosition.x + this.cpcs[9] * Time.deltaTime, this.cpcs[10], this.cpcs[11]); cameraPosition.y = Mathf.Clamp(cameraPosition.y + this.cpcs[12] * Time.deltaTime, this.cpcs[13], this.cpcs[14]); cameraPosition.z = Mathf.Clamp(cameraPosition.z + this.cpcs[15] * Time.deltaTime, this.cpcs[16], this.cpcs[17]); float d = Vector3.Distance(cameraPosition, new Vector3( this.cpcs[9] < 0 ? this.cpcs[10]: this.cpcs[11], this.cpcs[12] < 0 ? this.cpcs[13]: this.cpcs[14], this.cpcs[15] < 0 ? this.cpcs[16]: this.cpcs[17] )); if (d < 0.001f) { cameraMoveFinish = true; this.Invoke("nextShoot", 1.0f); } } else { cameraPosition.x = Mathf.Clamp(cameraPosition.y + this.cpcs[0] * Time.deltaTime, this.cpcs[1], this.cpcs[2]); cameraPosition.y = Mathf.Clamp(cameraPosition.y + this.cpcs[3] * Time.deltaTime, this.cpcs[4], this.cpcs[5]); cameraPosition.z = Mathf.Clamp(cameraPosition.z + this.cpcs[6] * Time.deltaTime, this.cpcs[7], this.cpcs[8]); } cameraT.localPosition = cameraPosition; cameraT.LookAt(this.transform.Find("Head")); } /**延时方法 */ public void nextShoot() { if (GameMgr.ins.checkFinish()) return; this.armBow.readyShoot(); Destroy(this.transform.Find("Camera").gameObject); // CrossHair.ins.gameObject.SetActive(true); this.enabled = false; } void OnCollisionEnter(Collision collision) { if ((1 << collision.gameObject.layer) != LayerMask.GetMask("Target")) { this.Hit(); GameMgr.ins.hitTarget(0); AudioMgr.ins.PlayCheer(false); } this.transform.SetParent(collision.transform.parent); } void activeEffectCyclone(bool value) { this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value); } void activeEffectBomb(bool value) { this.transform.Find("Head/EF_baodian").gameObject.SetActive(value); } void activeEffectTrail(bool value) { this.transform.Find("EF_tuowei").gameObject.SetActive(value); this.transform.Find("EF_tuowei/Trail").GetComponent().time = 1.6f / speed; } void updateEffectCyclone() { ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent(); if (ps.minParticleSize < 0.6f) { ps.minParticleSize += 0.03f; ps.maxParticleSize = ps.minParticleSize; } ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent(); if (ps1.minParticleSize < 1.2f) { ps1.minParticleSize += 0.06f; ps1.maxParticleSize = ps1.minParticleSize; } } public void calculateSpeed(Vector3 rayPoint) { speed = Mathf.Clamp(Vector3.Distance(rayPoint, this.getHeadPosition()) / 2, 6.6f, 16); } }