using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class Arrow : MonoBehaviour { private Rigidbody newRigidbody; public Vector3 shootOutPosition; public float flyTime = 0; public bool isHit = false; public ArmBow armBow; public static float speed = GameMgr.RealSizeToGameSize(60); void Awake() { GameMgr.ins.gameMode.PauseTimeCounting(this); } void Start() { newRigidbody = this.gameObject.AddComponent(); float speedCopy = speed; if (GameAssistUI.ins) { speedCopy *= (1 + GameAssistUI.ins.shootScaleValue); } //箭刚射出去时,角度往上抬一点 float distance = TargetBody.ins.GetDistance(); this.transform.Rotate(Vector3.left, Mathf.Clamp(distance / 50 * 20, 0, 20)); newRigidbody.velocity = this.transform.forward * speedCopy; newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic; Transform cameraTF = this.transform.Find("Camera"); cameraTF.gameObject.SetActive(true); cameraTF.gameObject.AddComponent().arrow = this; this.activeEffectTrail(true); } void OnDestroy() { GameMgr.ins.gameMode.ResumeTimeCounting(this); if (this.newRigidbody) { Destroy(this.newRigidbody); } } Vector3 position; Quaternion rotation; void FixedUpdate() { if (newRigidbody) { transform.forward = newRigidbody.velocity.normalized; } if (!isHit && flyTime >= 0) { flyTime += Time.deltaTime; this.position = this.transform.position; this.rotation = this.transform.rotation; if (flyTime > 4) { Destroy(gameObject); GameMgr.ins.gameMode.HitTarget(0); AudioMgr.ins.PlayCheer(false); nextShoot(); } this.UpdateRotate(); } // this.UpdateShake(); } public void Hit() { gameObject.GetComponent().enabled = false; if (newRigidbody) { newRigidbody.useGravity = false; newRigidbody.velocity = Vector3.zero; Destroy(newRigidbody); newRigidbody = null; } isHit = true; // this.Shake(); this.activeEffectCyclone(false); this.activeEffectBomb(true); this.activeEffectTrail(false); Debug.Log("Speed " + GameMgr.GameSizeToRealSize(Arrow.speed)); Debug.Log("Distance " + GameMgr.GameSizeToRealSize(Vector3.Distance(this.getHeadPosition(), shootOutPosition))); } 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; } //进入下一轮射击 bool hasDoneNextShoot = false; public void nextShoot() { if (hasDoneNextShoot) return; hasDoneNextShoot = true; GameMgr.ins.gameMode.ResumeTimeCounting(this); if (!GameMgr.ins.gameMode.DoNextShoot()) return; this.armBow.readyShoot(); } void OnCollisionEnter(Collision collision) { if ((1 << collision.gameObject.layer) != LayerMask.GetMask("Target")) { this.Hit(); GameMgr.ins.gameMode.HitTarget(0); AudioMgr.ins.PlayCheer(false); } this.transform.SetParent(collision.transform.parent); } public void activeEffectCyclone(bool value) { this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value); if (!value) return; ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent(); ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent(); DOTween.To(() => ps.minParticleSize, value => { ps.minParticleSize = value; ps.maxParticleSize = value; }, 0.4f, 0.6f); DOTween.To(() => ps1.minParticleSize, value => { ps1.minParticleSize = value; ps1.maxParticleSize = value; }, 0.8f, 0.6f); } 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; } }