96 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			96 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System.Collections;
 | |
| using System.Collections.Generic;
 | |
| using UnityEngine;
 | |
| using DG.Tweening;
 | |
| 
 | |
| public class Arrow : MonoBehaviour
 | |
| {
 | |
|     [SerializeField] float mSpeed = 50;
 | |
|     [SerializeField] GameObject mPfbExplode;
 | |
|     [SerializeField] AudioClip mShootSound;
 | |
|     [SerializeField] AudioClip mHitSound;
 | |
|     [SerializeField] OrbitType mOrbit = OrbitType.Straight; 
 | |
| 
 | |
|     private int mAtkVal;
 | |
| 
 | |
|     private Transform mTrans;
 | |
|     //for curve
 | |
|     private float mMoveTime = 0f;
 | |
|     private Vector3 mInitPos = Vector3.zero;
 | |
|     private Vector3 mTargetPos = Vector3.zero;
 | |
|     private Vector3 mDirection = Vector3.zero;
 | |
| 
 | |
|     private WarUnit mTarget = null;
 | |
|     private bool mIsActive = false;
 | |
| 
 | |
|     private void Awake()
 | |
|     {
 | |
|         mTrans = transform;
 | |
|     }
 | |
| 
 | |
|     private void Update()
 | |
|     {
 | |
|         if (!mIsActive)
 | |
|             return;
 | |
| 
 | |
|         if (mOrbit == OrbitType.Straight)
 | |
|         {
 | |
|             mTrans.position += Time.deltaTime * mSpeed * mDirection;
 | |
|             mMoveTime -= Time.deltaTime;
 | |
|             if (mMoveTime <= 0)
 | |
|             {
 | |
|                 Hit();
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public void Init(int pAtkVal, Vector3 pInitPos, Vector3 pTargetPos, WarUnit pTarget)
 | |
|     {
 | |
|         mAtkVal = pAtkVal;
 | |
| 
 | |
|         mInitPos = pInitPos;
 | |
|         mTargetPos = pTargetPos;
 | |
| 
 | |
|         mTrans.position = mInitPos;
 | |
|         
 | |
|         Vector3 v = mTargetPos - mInitPos;
 | |
|         mDirection = v.normalized;
 | |
|         mMoveTime = v.magnitude / mSpeed;
 | |
|         mTarget = pTarget;
 | |
|         AudioManager.Instance.PlaySound(mShootSound, 0.6f);
 | |
| 
 | |
|         if (mOrbit == OrbitType.Straight)
 | |
|         {
 | |
|             mTrans.LookAt(mTargetPos);
 | |
|         }
 | |
|         else
 | |
|         {
 | |
|             Vector3 tHandle = mInitPos + v * 0.5f + Vector3.up * v.magnitude * 0.5f;
 | |
| 
 | |
|             Vector3[] tMovePath = BezierUtils.GetBezierListN2(mInitPos, tHandle, mTargetPos, Mathf.RoundToInt(Mathf.Max(5, v.magnitude)));
 | |
|             mTrans.DOPath(tMovePath, mMoveTime).SetLookAt(0).SetEase(Ease.Linear).onComplete = Hit;
 | |
|         }
 | |
| 
 | |
|         mIsActive = true;
 | |
|     }
 | |
| 
 | |
|     private void Hit()
 | |
|     {
 | |
|         if (mPfbExplode != null)
 | |
|         {
 | |
|             Instantiate(mPfbExplode, mTargetPos, Quaternion.identity);
 | |
|         }
 | |
|         AudioManager.Instance.PlaySound(mHitSound, 0.6f);
 | |
| 
 | |
|         mIsActive = false;
 | |
|         mTarget.Hurt(mAtkVal);
 | |
|         Destroy(gameObject);
 | |
|     }
 | |
| }
 | |
| 
 | |
| public enum OrbitType
 | |
| {
 | |
|     Straight,
 | |
|     Bezier
 | |
| }
 |