본문 바로가기

Game/Unity

[unity3d]미사일 충돌 효과

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using UnityEngine;
using System.Collections;
 
public class WallCtrl : MonoBehaviour {
 
    public GameObject sparkEffect;
 
    void OnCollisionEnter(Collision coll)
    {
        if(coll.collider.tag == "BULLET")
        {
            Vector3 firePos = coll.gameObject.GetComponent<BulletCtrl>()
.firePos;
            Vector3 relativePos = firePos - coll.transform.position;
 
            Instantiate(sparkEffect,
                        coll.transform.position,
                        Quaternion.LookRotation(relativePos));
            Destroy(coll.gameObject);
        }
    }
}


입사 각도이 반대 벡터는 벡터의 빼기 연산으로 산출함( 발사 원점 벡터 - 충돌 지점 벡터) -> 충돌 지점에서 발사 원점을 바라보는 벡터가됨

입사각 = 충돌지점 - 발사원점
반대각 = 발사원점 - 충돌지점


               -------->

(발사원점)ㅁ                ㅁ(충돌지점)

                <-------


//입사각도의 반대 벡터 = 발사 원점 - 충돌지점

Vector3 relativePos = firePos - coll.transform.position;


입사각, 반사각, 법선벡터


당구게임에서 당구공이 벽에 충돌해 반대쪽으로 튕겨나가는 로직에 필요한것이 반사각이다.

반사각을 구하기 위해서는 입사각가 법선벡터가 필요함

입사각 = 충돌지점 - 출발지점

반사각 = Vector3.Reflect(입사각, 법선벡터) fh rngkftndlTdma


Vector3 incomingVector = coll.transform.position - firePos ;  //입사각

Vector3 inverseVector = - incomingVector; //입사각의 반대각

Vector3 normalVector= coll.contacts[0].normal; //법선벡터

Vector3 reflectVector = Vector3.Reflect(incomingVector, normalVector); //반사각



출처 : http://hyunity3d.tistory.com/574