| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- 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 RaycastHit absoluteRay;
- public float offsetAngle;
- private float mySpeed = 0;
- public bool simulatedParabola = false; //模拟抛物线
- public ArmBow armBow;
- public static float speed = GameMgr.RealSizeToGameSize(60);
- void Awake()
- {
- GameMgr.ins.gameMode.PauseTimeCounting(this);
- }
- void Start()
- {
- newRigidbody = this.gameObject.AddComponent<Rigidbody>();
- mySpeed = speed;
- if (GameAssistUI.ins) {
- mySpeed *= (1 + GameAssistUI.ins.shootScaleValue);
- }
- if (absoluteRay.transform && absoluteRay.transform.gameObject.name == "TargetBody") {
- //把瞄准点画成红圈,渲染在靶子上
- Transform redCircle = TargetBody.ins.transform.Find("RedCircle");
- redCircle.gameObject.SetActive(true);
- redCircle.transform.position = -redCircle.transform.forward * 0.001f + absoluteRay.point;
- }
- if (absoluteRay.transform && absoluteRay.transform.gameObject.name == "TargetBody") {
- SetUpBeforFly();
- }
- newRigidbody.velocity = this.transform.forward * mySpeed;
- newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
- newRigidbody.drag = 0;
- newRigidbody.angularDrag = 0;
- Transform cameraTF = this.transform.Find("Camera");
- cameraTF.gameObject.SetActive(true);
- cameraTF.gameObject.AddComponent<ArrowCamera>().arrow = this;
- this.activeEffectTrail(true);
- //是update运动,则禁用物理运动
- if (useUpdatePhysics) {
- this.GetComponent<BoxCollider>().enabled = false;
- Destroy(newRigidbody);
- }
- }
- void SetUpBeforFly()
- {
- CalculateParabolaAngle(absoluteRay.point);
- float maxAngleX = 5;
- if (!hasParabolaAngle) return;
- float absoluteAngleX = parabolaAngleInRadian / Mathf.PI * 180;
- if (absoluteAngleX > maxAngleX) {
- absoluteAngleX = maxAngleX;
- }
- float finalAngleX = absoluteAngleX + this.offsetAngle;
- float vy = Mathf.Sin(finalAngleX / 180 * Mathf.PI) * this.mySpeed;
- float vx = Mathf.Cos(finalAngleX / 180 * Mathf.PI) * this.mySpeed;
- float deltaX = Mathf.Sqrt(
- Mathf.Pow(absoluteRay.point.x - this.shootOutPosition.x, 2) +
- Mathf.Pow(absoluteRay.point.z - this.shootOutPosition.z, 2)
- );
- float deltaY = absoluteRay.point.y - this.shootOutPosition.y;
- float tx = deltaX / vx;
- float finalDeltaY = vy * tx + 0.5f * Physics.gravity.y * tx * tx;
- float dy = finalDeltaY - deltaY;
- if (Mathf.Abs(dy) < 0.62f) { //再靶子范围内,则用update运算物理
- finalPoint = absoluteRay.point;
- finalPoint.y += dy;
- useUpdatePhysics = true;
- parabolaAngleInRadian = finalAngleX / 180 * Mathf.PI;
- RandomUseSideCamera();
- } else {
- Vector3 eulerAngle = this.transform.eulerAngles;
- eulerAngle.x = -finalAngleX;
- this.transform.eulerAngles = eulerAngle;
- }
- }
- void RandomUseSideCamera() {
- //有几率进入侧面镜头观看
- float distance = TargetBody.ins.GetDistance();
- if (Mathf.RoundToInt(distance) >= 50) {
- if (Random.value < 0.5) simulatedParabola = true;
- }
- }
- bool hasParabolaAngle = false;
- float parabolaAngleInRadian = 0;
- void CalculateParabolaAngle(Vector3 destination)
- {
- float deltaX = Vector2.Distance(
- new Vector2(destination.x, destination.z),
- new Vector2(this.transform.position.x, this.transform.position.z)
- );
- float deltaY = destination.y - this.transform.position.y;
- float a = 0.5f * Physics.gravity.y * Mathf.Pow(deltaX, 2) / Mathf.Pow(this.mySpeed, 2);
- float b = deltaX;
- float c = a - deltaY;
- hasParabolaAngle = Mathf.Pow(b, 2) - 4 * a * c >= 0;
- if (hasParabolaAngle) {
- float res1 = (-b + Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
- float res2 = (-b - Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
- parabolaAngleInRadian = Mathf.Min(Mathf.Atan(res1), Mathf.Atan(res2));
- }
- }
- 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 > 14) {
- Destroy(gameObject);
- GameMgr.ins.gameMode.HitTarget(0);
- AudioMgr.ins.PlayCheer(false);
- nextShoot();
- }
- this.UpdateRotate();
- }
- // this.UpdateShake();
- }
- void Update() {
- UpdateFlyWithoutPhysics();
- }
- bool useUpdatePhysics = false;
- float flyTimeWithoutPhysics = 0;
- Vector3 finalPoint = default;
- void UpdateFlyWithoutPhysics() {
- if (!useUpdatePhysics || isHit) return;
- flyTimeWithoutPhysics += Time.deltaTime;
- Vector3 destination = finalPoint;
- float deltaX = Mathf.Sqrt(
- Mathf.Pow(destination.x - shootOutPosition.x, 2) +
- Mathf.Pow(destination.z - shootOutPosition.z, 2)
- );
- float vx = Mathf.Cos(parabolaAngleInRadian) * mySpeed;
- float t = deltaX / vx;
- if (flyTimeWithoutPhysics < t) {
- t = flyTimeWithoutPhysics;
- }
- float vy = Mathf.Sin(parabolaAngleInRadian) * mySpeed + Physics.gravity.y * t;
- float dy = Mathf.Sin(parabolaAngleInRadian) * mySpeed * t + 0.5f * Physics.gravity.y * Mathf.Pow(t, 2);
- float dx = vx * t;
- Vector3 nextPosition = new Vector3(destination.x - shootOutPosition.x, 0, destination.z - shootOutPosition.z);
- nextPosition = nextPosition.normalized * dx;
- nextPosition.y = shootOutPosition.y + dy;
- this.transform.position = nextPosition;
- Vector3 eulerAngles = this.transform.eulerAngles;
- float angleX = Mathf.Atan(vy / vx) / Mathf.PI * 180;
- eulerAngles.x = -angleX;
- this.transform.eulerAngles = eulerAngles;
- //箭头到达目标点时,触发碰撞(之所以用以下,是因为箭的锚点在尾部,而箭的长度接近1.1)
- if (Vector3.Distance(nextPosition, destination) < 1.1f) {
- this.transform.SetParent(TargetBody.ins.transform);
- TargetBody.ins.Hit(this, destination);
- }
- }
- public void Hit() {
- GameDebug.ins.ShowRes(absoluteRay.point, this.getHeadPosition());
- 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);
- //最新一箭击中后会发光标记
- ArrowLightSick.RecoveryAll();
- this.GetComponentInChildren<ArrowLightSick>().Hit();
- }
- 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;
- }
- //进入下一轮射击
- public 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();
- //把瞄准点画成红圈,渲染在靶子上(取消)
- Transform redCircle = TargetBody.ins.transform.Find("RedCircle");
- redCircle.gameObject.SetActive(false);
- //最新一箭击中后会发光标记(取消)
- ArrowLightSick.RecoveryAll();
- }
- 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>();
- ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
- 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<TrailRenderer>().time = 1.6f / mySpeed;
- }
- }
|