OldBlueWater/BlueWater/Assets/02.Scripts/Weapon/ParticleWeapon.cs
NTG_Lenovo 43eb378a26 #34 #35 InIslandPlayer, Crewmate, Enemy 조정
+ 03.Stage_Test 간단한 지형지물 추가
+ 캐릭터 HpSlider 기능 추가 및 연동
+ UiCanvas 추가(캐릭터 HpSlider 관리)
+ Sorting Layer(UI) 추가
+ FX 파티클 ParticleWeapon 스크립트로 관리
  ㄴ MagicOrk의 Fireball

fixed
+ InIslandPlayer가 스폰되고 나서 카메라가 흔들리거나 내려가는 버그 수정
2023-10-20 14:34:30 +09:00

88 lines
3.9 KiB
C#

using UnityEngine;
// ReSharper disable once CheckNamespace
namespace BlueWaterProject
{
public class ParticleWeapon : MonoBehaviour
{
public GameObject impactParticle; // Effect spawned when projectile hits a collider
public GameObject projectileParticle; // Effect attached to the gameobject as child
public GameObject muzzleParticle; // Effect instantly spawned when gameobject is spawned
[Header("Adjust if not using Sphere Collider")]
public float colliderRadius = 1f;
[Range(0f, 1f)] // This is an offset that moves the impact effect slightly away from the point of impact to reduce clipping of the impact effect
public float collideOffset = 0.15f;
[SerializeField] private LayerMask targetLayer;
[SerializeField] private float power;
private Rigidbody rb;
private SphereCollider sphereCollider;
private void Awake()
{
rb = GetComponent<Rigidbody>();
sphereCollider = GetComponent<SphereCollider>();
}
private void Start()
{
projectileParticle = Instantiate(projectileParticle, transform.position, transform.rotation, transform) as GameObject;
projectileParticle.transform.parent = transform;
if (muzzleParticle)
{
muzzleParticle = Instantiate(muzzleParticle, transform.position, transform.rotation, transform) as GameObject;
Destroy(muzzleParticle, 1.5f); // 2nd parameter is lifetime of effect in seconds
}
}
private void FixedUpdate()
{
if (rb.velocity.magnitude != 0)
{
transform.rotation = Quaternion.LookRotation(rb.velocity); // Sets rotation to look at direction of movement
}
float radius; // Sets the radius of the collision detection
if (sphereCollider)
radius = sphereCollider.radius;
else
radius = colliderRadius;
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, radius, direction, out var hit, detectionDistance, targetLayer)) // Checks if collision will happen
{
transform.position = hit.point + (hit.normal * collideOffset); // Move projectile to point of collision
var impactP = Instantiate(impactParticle, transform.position, Quaternion.FromToRotation(Vector3.up, hit.normal), transform) as GameObject; // Spawns impact effect
var trails = GetComponentsInChildren<ParticleSystem>(); // Gets a list of particle systems, as we need to detach the trails
//Component at [0] is that of the parent i.e. this object (if there is any)
for (var i = 1; i < trails.Length; i++) // Loop to cycle through found particle systems
{
var trail = trails[i];
if (trail.gameObject.name.Contains("Trail"))
{
trail.transform.SetParent(null); // Detaches the trail from the projectile
Destroy(trail.gameObject, 2f); // Removes the trail after seconds
}
}
Destroy(projectileParticle, 3f); // Removes particle effect after delay
Destroy(impactP, 3.5f); // Removes impact effect after delay
Destroy(gameObject); // Removes the projectile
hit.transform.GetComponent<IDamageable>()?.TakeDamage(power);
}
}
public void SetPower(float value) => power = value;
}
}