RemoveBullet
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RemoveBullet : MonoBehaviour
{
[SerializeField]
private GameObject sparkEffect;
private void OnCollisionEnter(Collision collision)
{
Debug.Log(collision);
//if(collision.collider.CompareTag("Bullet"))
//{
//}
if(collision.collider.tag == "Bullet")
{
//스파크 파티클을 동적으로 생성
//Instantiate(sparkEffect, collision.transform.position, Quaternion.identity);
Debug.LogFormat("collision.contactCount: {0}", collision.contactCount);
for(int i = 0; i < collision.contactCount; i++)
{
ContactPoint contactPoint = collision.GetContact(i);
Debug.LogFormat("<color=cyan>=> {0}</color>", contactPoint.point);
}
//충돌지점의 법선벡터를 구하기 위함
ContactPoint cp = collision.GetContact(0);
DrawArrow.ForDebug(cp.point, -1 * cp.normal, 5f, Color.green, ArrowType.Solid);
//벡터 뒤집기
var rot = Quaternion.LookRotation(-1 * cp.normal);
//Instantiate(this.sparkEffect, cp.point, rot);
GameObject spark = Instantiate(this.sparkEffect, cp.point, rot);
Destroy(spark, 1f);
Destroy(collision.gameObject);
}
}
}
BarrelController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BarrelController : MonoBehaviour
{
private int hitCount = 0;
[SerializeField]
private GameObject expEffect; //프리팹
private Rigidbody rBody;
[SerializeField]
private Texture[] textures;
private new MeshRenderer renderer; // new를 안쓰면 부모가 이미 쓰고있다.
private void Start()
{
this.rBody = this.GetComponent<Rigidbody>();
int idx = Random.Range(0, this.textures.Length); // 0, 1, 2
this.renderer = this.gameObject.GetComponentInChildren<MeshRenderer>();
this.renderer.material.mainTexture = this.textures[idx];
}
private void OnCollisionEnter(Collision collision)
{
if(collision.collider.CompareTag("Bullet"))
{
//횟수를 증가
this.hitCount++;
Debug.LogFormat("{0}회 맞았다!", this.hitCount);
if(this.hitCount >= 3)
{
this.ExpBarrel();
}
}
}
void ExpBarrel()
{
Debug.Log("폭파");
//연출
//동적으로 폭파 이펙트 생성하기
GameObject exp = Instantiate(this.expEffect, this.transform.position, Quaternion.identity);
Destroy(exp, 0.5f);
//위로 힘줘서 날려버리기
this.rBody.mass = 1f; //무게를 가볍게
this.rBody.AddForce(Vector2.up * 1500f);
//간접 폭발력 전달
this.IndirectDamage(this.transform.position);
//3초후 드럼통 제거
Destroy(this.gameObject, 3.0f);
}
private void IndirectDamage(Vector3 position)
{
//주변에 있는 모든 드럼통을 추출한다.
Collider[] colls = Physics.OverlapSphere(position, this.radius, 1 << 3); //레이어 3번을 의미
foreach(Collider col in colls)
{
var rb = col.GetComponent<Rigidbody>();
rb.mass = 1f;
rb.constraints = RigidbodyConstraints.None;
rb.AddExplosionForce(1500, position, 1200);
}
}
[SerializeField]
private float radius = 1f;
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(this.transform.position, this.radius);
}
}
'유니티 심화' 카테고리의 다른 글
OverlapSphere[수정필] (0) | 2023.08.21 |
---|---|
Reflect (0) | 2023.08.21 |
절대강좌 유니티 - Quaternion.LookRotation (0) | 2023.08.21 |
절대강좌 유니티 - 총알 이펙트효과 (0) | 2023.08.21 |
[주말과제] - 궁수의 전설[완] (0) | 2023.08.19 |