You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
5.4 KiB
147 lines
5.4 KiB
using DG.Tweening; |
|
using System.Collections; |
|
using System.Collections.Generic; |
|
using UDE_HAND_INTERACTION; |
|
using UnityEngine; |
|
|
|
public class BowActiveSample : MonoBehaviour |
|
{ |
|
private GameObject Handle; |
|
public Vector3 MotionDirection = Vector3.forward; |
|
public GameObject[] Pivots; |
|
public float MaxRotation; |
|
public Vector3 RotDirection = Vector3.forward; |
|
public float MinDistance = -0.14f; |
|
public float MaxDistance = -0.43f; |
|
public Vector3 ExtendDirection = Vector3.forward; |
|
public float MaxExtraScale = 0.2f; |
|
|
|
private GameObject Arrow; |
|
public Queue<GameObject> ArrowPool = new(); |
|
private bool Inshooting = false; |
|
private bool ArrowInEquip = false; |
|
|
|
private Transform FollowedHand = null; |
|
private Vector3 PositionOffset = Vector3.zero; |
|
|
|
public AudioSource TightString; |
|
public AudioSource ArrrowShoot; |
|
|
|
void Start() |
|
{ |
|
Application.targetFrameRate = 200; |
|
Handle = GetComponent<InteractableObject>().InsideOperationPart.gameObject; |
|
} |
|
|
|
// Update is called once per frame |
|
void Update() |
|
{ |
|
FollowedHand = GetComponent<InteractableObject>().GetReferenceHand(1); |
|
var FakeHand = GetComponent<InteractableObject>().GetReferenceHand(0); |
|
if (FakeHand && !FakeHand.name.Contains("Release")) |
|
{ |
|
PositionOffset = FakeHand.transform.position - Handle.transform.position; |
|
if (FollowedHand != null) |
|
{ |
|
Handle.transform.position = FollowedHand.transform.position - PositionOffset; |
|
} |
|
} |
|
|
|
float ActiveRate = (Vector3.Dot(Handle.transform.localPosition, MotionDirection) - MinDistance) / (MaxDistance - MinDistance); |
|
|
|
Pivots[0].transform.localEulerAngles = RotDirection * ActiveRate * MaxRotation; |
|
Pivots[1].transform.localEulerAngles = -RotDirection * ActiveRate * MaxRotation; |
|
|
|
Pivots[0].transform.localScale = Vector3.one + ExtendDirection * ActiveRate * MaxExtraScale; |
|
Pivots[1].transform.localScale = Vector3.one + ExtendDirection * ActiveRate * MaxExtraScale; |
|
|
|
Handle.transform.localPosition = new Vector3(0, 0, Handle.transform.localPosition.z); |
|
|
|
if (Vector3.Dot(Handle.transform.localPosition, MotionDirection) < MaxDistance) |
|
{ |
|
Handle.transform.localPosition = MotionDirection * MaxDistance; |
|
Pivots[0].transform.localEulerAngles = RotDirection * MaxRotation; |
|
Pivots[1].transform.localEulerAngles = -RotDirection * MaxRotation; |
|
|
|
Pivots[0].transform.localScale = Vector3.one + ExtendDirection * MaxExtraScale; |
|
Pivots[1].transform.localScale = Vector3.one + ExtendDirection * MaxExtraScale; |
|
} |
|
else if (Vector3.Dot(Handle.transform.localPosition, MotionDirection) >= MinDistance) |
|
{ |
|
Handle.transform.localPosition = MotionDirection * MinDistance; |
|
Pivots[0].transform.localEulerAngles = Vector3.zero; |
|
Pivots[1].transform.localEulerAngles = Vector3.zero; |
|
|
|
Pivots[0].transform.localScale = Vector3.one; |
|
Pivots[1].transform.localScale = Vector3.one; |
|
} |
|
|
|
if(ActiveRate <= 0.6f && ActiveRate != 0 && !FakeHand.name.Contains("Release")) |
|
{ |
|
if(TightString != null) TightString.Play(); |
|
} |
|
|
|
if ((PositionOffset != Vector3.zero && FakeHand.name.Contains("Release")) || Input.GetKeyDown(KeyCode.F1)) |
|
{ |
|
Inshooting = true; |
|
Handle.transform.DOLocalMove(MotionDirection * MinDistance, 0.03f); |
|
PositionOffset = Vector3.zero; |
|
if (Arrow != null && ActiveRate >= 0.6f && ArrowInEquip) |
|
{ |
|
Arrow.transform.GetPositionAndRotation(out Vector3 CurPos, out Quaternion CurRot); |
|
var ShootArrow = Instantiate(Arrow, null, true); |
|
ShootArrow.transform.SetPositionAndRotation(CurPos, CurRot); |
|
Destroy(Arrow); |
|
Arrow = ShootArrow; |
|
|
|
if (ArrrowShoot != null) ArrrowShoot?.Play(); |
|
var rigid = Arrow.GetComponent<Rigidbody>(); |
|
rigid.velocity = Arrow.transform.forward * 22f * ActiveRate; |
|
rigid.isKinematic = false; |
|
rigid.useGravity = true; |
|
Invoke(nameof(ResetArrow), 0.1f); |
|
} |
|
else |
|
{ |
|
Inshooting = false; |
|
} |
|
} |
|
} |
|
|
|
private void OnTriggerStay(Collider other) |
|
{ |
|
if (other.name == "ArrowBody" && !Inshooting) |
|
{ |
|
Transform ArrowTrans = other.gameObject.transform.parent.parent; |
|
Arrow = ArrowTrans.gameObject; |
|
if (!Arrow.GetComponent<InteractableObject>().IsFollow && !ArrowInEquip) |
|
{ |
|
ArrowTrans.parent = Handle.transform; |
|
ArrowTrans.DOLocalMove(Vector3.zero, 0.1f); |
|
ArrowTrans.DOLocalRotate(Vector3.zero, 0.1f); |
|
ArrowInEquip = true; |
|
Arrow.GetComponent<InteractableObject>().enabled = false; |
|
} |
|
} |
|
} |
|
private void ResetArrow() |
|
{ |
|
Inshooting = false; |
|
ArrowInEquip = false; |
|
Arrow?.GetComponent<ArrowTipSample>().Reset(); |
|
if (ArrowPool.Count == 12) |
|
{ |
|
Destroy(ArrowPool.Dequeue()); |
|
} |
|
ArrowPool.Enqueue(Arrow); |
|
//Destroy(Arrow); |
|
Arrow = null; |
|
} |
|
|
|
public void ResetParam() |
|
{ |
|
Inshooting = false; |
|
ArrowInEquip = false; |
|
Arrow = null; |
|
} |
|
}
|
|
|