1. 화살이 충돌하는 과정에서 버벅이는 버그 수정
2. 원거리 Ai(궁수) 공격 충돌 테스트 완료
3. 화살을 발사하는 구조 변경
기존 : 애니메이션 실행 후 일정 시간이 지나면 화살을 발사(렉걸리는 경우 싱크가 안맞음)
변경 후 : 애니메이션 클립을 복제해 애니메이션에 event를 추가하여 일정한 순간에 화살을 발사하게 변경(싱크 맞춤)
This commit is contained in:
NTG 2023-08-11 03:03:23 +09:00
parent 86065e52a9
commit 88f970c2d9
9 changed files with 75145 additions and 188 deletions

File diff suppressed because it is too large Load Diff

View File

@ -15,10 +15,7 @@ namespace BlueWaterProject
[Tooltip("화살 오브젝트 풀링할 최대 갯수")]
[SerializeField] private int arrowMaxSize;
[Tooltip("화살 발사 시작 시간")]
[SerializeField] private float shootArrowTime;
[Tooltip("화살 발사 위치")]
[SerializeField] private Transform shootLocation;
@ -76,20 +73,27 @@ namespace BlueWaterProject
{
while (true)
{
if (!fieldOfView.GetTargetInfo().transform) yield break;
if (!fieldOfView.GetTargetInfo().transform)
{
yield break;
}
aiAnimator.SetTrigger(AttackHash);
yield return new WaitForSeconds(shootArrowTime);
if (!fieldOfView.GetTargetInfo().transform) yield break;
var arrow = arrowPool.Get();
arrow.SetShootingArrow(shootLocation.position,
fieldOfView.GetTargetInfo().collider.bounds.center, AiStat, attackerType, inaccuracy);
StartCoroutine(arrow.Shoot());
yield return new WaitForSeconds(AiStat.atkCooldown);
}
}
/// <summary>
/// Archer attack 애니메이션에 event 부착용 함수
/// </summary>
public void ShootArrow()
{
var arrow = arrowPool.Get();
arrow.SetShootingArrow(shootLocation.position,
fieldOfView.GetTargetInfo().collider.bounds.center, AiStat, attackerType, inaccuracy);
StartCoroutine(arrow.Shoot());
}
#endregion

View File

@ -18,9 +18,9 @@ namespace BlueWaterProject
[SerializeField] private float arrowSpeed = 15f;
private float g = Mathf.Abs(Physics.gravity.y);
private float inaccuracy;
private Vector3 targetPos;
private AttackerType attackerType;
private float inaccuracy;
private Transform attackerTransform;
private AiStat attackerStat;
@ -38,7 +38,6 @@ namespace BlueWaterProject
private void OnTriggerEnter(Collider other)
{
print(other.gameObject.name);
if (other.gameObject.layer == LayerMask.NameToLayer("Ground") ||
other.gameObject.layer == LayerMask.NameToLayer("Water"))
{
@ -46,6 +45,7 @@ namespace BlueWaterProject
}
else if (other.gameObject.layer == LayerMask.NameToLayer("HitBox"))
{
print(attackerType);
switch (attackerType)
{
case AttackerType.NONE:
@ -72,8 +72,7 @@ namespace BlueWaterProject
throw new ArgumentOutOfRangeException();
}
var iDamageable = other.transform.parent.GetComponent<IDamageable>();
print(iDamageable);
iDamageable.TakeDamage(attackerStat, iDamageable.AiStat);
DestroyObject();
}
@ -86,35 +85,38 @@ namespace BlueWaterProject
public IEnumerator Shoot()
{
var time = 0f;
var inaccuracyOffset = new Vector3(
// 화살의 목표 지점에 대한 불확실성 위치 값
var inaccuracyOffset = new Vector3
(
UnityEngine.Random.Range(-inaccuracy, inaccuracy),
UnityEngine.Random.Range(-inaccuracy, inaccuracy),
UnityEngine.Random.Range(-inaccuracy, inaccuracy)
);
// 화살의 최종 목표 지점
var inaccurateTargetPos = targetPos + inaccuracyOffset;
transform.rotation = Quaternion.LookRotation(inaccurateTargetPos);
transform.rotation *= Quaternion.Euler(0f, -90f, 0f);
var myPos = transform.position;
// 화살이 날아가야 하는 초기 수평 방향
var toTargetFlat = new Vector3(inaccurateTargetPos.x - myPos.x, 0, inaccurateTargetPos.z - myPos.z);
// 발사될 때의 화살과 목표 지점 간의 수평 거리
var horizontalDistance = toTargetFlat.magnitude;
// 발사될 때의 화살과 목표 지점 간의 수직 거리
var yOffset = inaccurateTargetPos.y - myPos.y;
// 화살이 목표 지점에 도착하는 데 예상되는 시간
var timeToTarget = horizontalDistance / arrowSpeed;
// 화살의 초기 수직 속도
var initialVerticalSpeed = (yOffset + (0.5f * g * timeToTarget * timeToTarget)) / timeToTarget;
// 화살의 발사 속도
var launchVelocity = toTargetFlat.normalized * arrowSpeed + Vector3.up * initialVerticalSpeed;
arrowRigidbody.velocity = launchVelocity;
transform.rotation = Quaternion.LookRotation(arrowRigidbody.velocity) * Quaternion.Euler(0f, -90f, 0f);
while (time < autoDestroyTime)
{
var myPos = transform.position;
var toTargetFlat = new Vector3(inaccurateTargetPos.x - myPos.x, 0, inaccurateTargetPos.z - myPos.z);
var horizontalDistance = toTargetFlat.magnitude;
var timeToTarget = horizontalDistance / arrowSpeed;
var yOffset = targetPos.y - myPos.y;
var initialVerticalSpeed = (yOffset + (0.5f * g * timeToTarget * timeToTarget)) / timeToTarget;
time += Time.deltaTime;
var launchVelocity = toTargetFlat.normalized * arrowSpeed + Vector3.up * initialVerticalSpeed;
arrowRigidbody.velocity = launchVelocity;
transform.rotation = Quaternion.LookRotation(arrowRigidbody.velocity);
transform.rotation *= Quaternion.Euler(0f, -90f, 0f);
transform.rotation = Quaternion.LookRotation(arrowRigidbody.velocity) * Quaternion.Euler(0f, -90f, 0f);
yield return null;
}
@ -124,6 +126,9 @@ namespace BlueWaterProject
}
}
/// <summary>
/// 화살이 발사 되기 직전에 필요한 데이터들을 입력받는 함수
/// </summary>
public void SetShootingArrow(Vector3 shootPos, Vector3 targetPosition, AiStat attackerAiStat, AttackerType type, float inaccuracyValue)
{
transform.position = shootPos;
@ -146,9 +151,11 @@ namespace BlueWaterProject
#region Custom function
/// <summary>
/// objectPool 시스템에 의해서 Release되는 경우 값을 초기화 시키는 함수
/// </summary>
public void ReleaseArrowSetting()
{
StopAllCoroutines();
gameObject.SetActive(false);
arrowRigidbody.velocity = Vector3.zero;
arrowRigidbody.angularVelocity = Vector3.zero;

View File

@ -146,9 +146,5 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: b92a5d3b88920fb49a3f6cbb978d4256, type: 3}
m_Name:
m_EditorClassIdentifier:
inaccuracy: 0.2
autoDestroyTime: 5
targetLayer:
serializedVersion: 2
m_Bits: 0
arrowSpeed: 15

View File

@ -237,7 +237,7 @@ AnimatorState:
m_MirrorParameterActive: 0
m_CycleOffsetParameterActive: 0
m_TimeParameterActive: 0
m_Motion: {fileID: 7400000, guid: 5142e855d0214a64fa6432eafc50a2d0, type: 3}
m_Motion: {fileID: 7400000, guid: 90574d0435d9bb14eb9aeb2320225b2e, type: 2}
m_Tag:
m_SpeedParameter:
m_MirrorParameter:
@ -286,7 +286,7 @@ AnimatorState:
m_MirrorParameterActive: 0
m_CycleOffsetParameterActive: 0
m_TimeParameterActive: 0
m_Motion: {fileID: 7400000, guid: 2f2a39086989dc44b8d626d1c663ed0d, type: 3}
m_Motion: {fileID: 7400000, guid: ba48d8d91afce6b4684270b9600d1451, type: 2}
m_Tag:
m_SpeedParameter:
m_MirrorParameter:
@ -379,7 +379,7 @@ AnimatorController:
m_SyncedLayerAffectsTiming: 0
m_Controller: {fileID: 9100000}
- serializedVersion: 5
m_Name: UpperBody
m_Name: R
m_StateMachine: {fileID: 1014360538280732316}
m_Mask: {fileID: 31900000, guid: ad8fbe0bee0314f4b845e2f964358778, type: 2}
m_Motions: []
@ -459,7 +459,7 @@ AnimatorStateMachine:
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: UpperBody
m_Name: R
m_ChildStates:
- serializedVersion: 1
m_State: {fileID: 7758239304624801405}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ba48d8d91afce6b4684270b9600d1451
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 7400000
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 90574d0435d9bb14eb9aeb2320225b2e
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 7400000
userData:
assetBundleName:
assetBundleVariant: