[Unity] Collider의 속성과 메서드

https://www.youtube.com/watch?v=79b9vFMOi5w&list=PLUZ5gNInsv_PR72-V9bTABaZu2py4DJq8&index=4 

위 영상을 바탕으로 작성하였습니다.

 


Is Trigger : 충돌 감지. 물리 효과는 없음. 그러므로 체크하면 충돌은 감지하지만 다른 물체를 통과한다.

 

 

Material : Physic Material은 물리와 관련된 Material이다.

오른쪽마우스 - Create - Physics Material에서 만들 수 있다.

 

Dynamic Friction - 마찰

Static Friction - 마찰

Bounciness - 탄성

Friction Combine - Average면 Friction이 점점 줄어든다.

Bounciness Combine - Average면 Bounciness가 점점 줄어든다.

 

예를 들어 바닥과 충돌하면 튕기는 공을 구현하고 싶을 때 Bounciness를 1로 높인 Physics Material을 만든 후 공 오브젝트에 적용시켜 구현할 수 있다.

 


< 스크립트에서 collider 쓰기 >

private BoxCollider col;

void Start(){
	col = GetComponent<BoxCollider>();
}

void Update(){
	if(Input.GetKeyDown(KeyCode.W)){
    	// center는 transform의 position + center, extents는 size의 절반
    	Debug.Log("col.bounds" + col.bounds); 
        Debug.Log("col.bounds.extents" + col.bounds.extents); // size의 절반
        Debug.Log("col.bounds.extents.x" + col.bounds.extents.x); // x만 나옴
        Debug.Log("col.size" + col.size); // size 나옴
        Debug.Log("col.center" + col.center); // center나옴
        
        col.size = new Vector3(3,3,3) // 이런식으로 크기 수정 가능
    }
}

 

< 유용한 메서드 >

Raycast

private BoxCollider col;

void Start(){
	col = GetComponent<BoxCollider>();
}

void Update(){
	if(Input.GetMouseButtonDown(0)){ // 마우스 좌클릭 하면
    	// Camera.main : tag가 Main Camera인 카메라
        // Input.mousePosition의 좌표에 ScreenPointToRay로 ray를 쏜다.
    	Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;
        if(col.Raycast(ray, out hitInfo, 1000)){ // ray가 col에 닿으면
        	this.transform.position = hitInfo.point; // hitInfo.point로 좌표 이동
        }
	}
}

 

(물리효과X)

OnTriggerEnter

OnTriggerExit

OnTriggerStay

(물리효과O)

OnCollisionEnter

OnCollisionExit

OnCollisionStay

이건 많이 쓰는 거니까 패스...