| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- using System.Collections.Generic;
- using UnityEngine;
- using DG.Tweening;
- public class Arrow : MonoBehaviour
- {
- private Rigidbody newRigidbody;
- public float flyTime = 0;
- public bool isHit = false;
- public ArmBow armBow;
- public static float speed = 40;
- //镜头跟随飞出的箭矢
- public static bool followArrow = true;
- void Start()
- {
- // CrossHair.ins.gameObject.SetActive(false);
- newRigidbody = this.gameObject.AddComponent<Rigidbody>();
- float speedCopy = speed;
- if (GameAssistUI.ins) {
- speedCopy *= (1 + GameAssistUI.ins.shootScaleValue);
- }
- newRigidbody.velocity = this.transform.forward * speedCopy;
- newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
- this.transform.Find("Camera").gameObject.SetActive(followArrow);
- this.activeEffectCyclone(true);
- this.activeEffectTrail(true);
- if (!followArrow) {
- GameObject.FindObjectOfType<BowCamera>().onViewRecovery = nextShoot;
- }
- }
- void OnDestroy() {
- 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);
- if (followArrow) {
- nextShoot();
- }
- }
- this.UpdateRotate();
- }
- this.UpdateShake();
- this.updateMoveCamera();
- }
- public void Hit() {
- gameObject.GetComponent<BoxCollider>().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);
- }
- 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;
- if (followArrow) {
- Sequence seq = DOTween.Sequence();
- seq.AppendInterval(1.0f);
- seq.AppendCallback(delegate() {
- nextShoot();
- Destroy(cameraT.gameObject);
- this.enabled = false;
- });
- }
- }
- } 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.gameMode.DoNextShoot()) return;
- this.armBow.readyShoot();
- // CrossHair.ins.gameObject.SetActive(true);
- }
- 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);
- }
- void activeEffectCyclone(bool value)
- {
- this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
- ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
- ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
- if (!followArrow) {
- ps.minParticleSize = 0.3f;
- ps.maxParticleSize = 0.3f;
- ps1.minParticleSize = 0.6f;
- ps1.maxParticleSize = 0.6f;
- }
- DOTween.To(() => ps.minParticleSize, value => {
- ps.minParticleSize = value;
- ps.maxParticleSize = value;
- }, followArrow ? 0.6f : 0.06f, 0.6f);
- DOTween.To(() => ps1.minParticleSize, value => {
- ps1.minParticleSize = value;
- ps1.maxParticleSize = value;
- }, followArrow ? 1.2f : 0.12f, 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<TrailRenderer>().time = 1.6f / speed;
- }
- }
|