| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- 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 = 40;
- void Start()
- {
- // CrossHair.ins.gameObject.SetActive(false);
- newRigidbody = this.gameObject.AddComponent<Rigidbody>();
- newRigidbody.velocity = this.transform.forward * speed;
- newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
- this.transform.Find("Camera").gameObject.SetActive(true);
- this.activeEffectCyclone(true);
- this.activeEffectTrail(true);
- }
- void OnDestroy() {
- if (this.newRigidbody != null)
- {
- Destroy(this.newRigidbody);
- }
- }
- 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<BoxCollider>().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();
- this.Invoke("destroySelf", 0.05f);
- // CrossHair.ins.gameObject.SetActive(true);
- }
- void destroySelf() {
- Destroy(this.transform.Find("Camera").gameObject);
- 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<TrailRenderer>().time = 1.6f / speed;
- }
- void updateEffectCyclone() {
- ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
- if (ps.minParticleSize < 0.6f)
- {
- ps.minParticleSize += 0.03f;
- ps.maxParticleSize = ps.minParticleSize;
- }
- ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
- if (ps1.minParticleSize < 1.2f)
- {
- ps1.minParticleSize += 0.06f;
- ps1.maxParticleSize = ps1.minParticleSize;
- }
- }
- }
|