OldBlueWater/BlueWater/Assets/02.Scripts/Weapon/ObjectWeapon.cs
NTG_Lenovo 3c2e8d68bd closed #39 오브젝트 형식의 원거리 공격(ObjectWeapon) 구현
+ 파티클, 오브젝트 형식의 공격에서 autoDestory추가
  ㄴ 자동으로 autoDestoryTime이 지난 후 파괴
+ TenTen(원거리 무기) Dagger 추가
2023-10-23 11:48:27 +09:00

70 lines
2.2 KiB
C#

using System.Collections;
using UnityEngine;
// ReSharper disable once CheckNamespace
namespace BlueWaterProject
{
public class ObjectWeapon : MonoBehaviour
{
[SerializeField] private LayerMask targetLayer;
[SerializeField] private float power;
[SerializeField] private float autoDestroyTime = 3f;
private Rigidbody rb;
private SphereCollider sphereCollider;
private WaitForSeconds waitForSeconds;
private void Awake()
{
rb = GetComponent<Rigidbody>();
sphereCollider = GetComponent<SphereCollider>();
waitForSeconds = new WaitForSeconds(autoDestroyTime);
}
private void OnDestroy()
{
StopAllCoroutines();
}
private void Start()
{
StartCoroutine(nameof(AutoDestroy));
}
private void FixedUpdate()
{
// if (rb.velocity.magnitude != 0)
// {
// transform.rotation = Quaternion.LookRotation(rb.velocity); // Sets rotation to look at direction of movement
// }
var direction = rb.velocity; // Gets the direction of the projectile, used for collision detection
if (rb.useGravity)
direction += Physics.gravity * Time.deltaTime; // Accounts for gravity if enabled
direction = direction.normalized;
var detectionDistance = rb.velocity.magnitude * Time.deltaTime; // Distance of collision detection for this frame
if (Physics.SphereCast(transform.position, sphereCollider.radius, direction, out var hit, detectionDistance, targetLayer)) // Checks if collision will happen
{
transform.position = hit.point + (hit.normal); // Move projectile to point of collision
Destroy(gameObject); // Removes the projectile
hit.transform.GetComponent<IDamageable>()?.TakeDamage(power);
}
}
private IEnumerator AutoDestroy()
{
yield return waitForSeconds;
if (gameObject != null)
{
Destroy(gameObject);
}
}
public void SetPower(float value) => power = value;
}
}