박스 콜라이더, 레이캐스트, OnTriggerEnter, OnCollisionEnter
박스 콜라이더(box collider)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private BoxCollider col;
private void Start()
{
col = GetComponent<BoxCollider>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
Debug.Log("col.bounds" + col.bounds);
Debug.Log("col.bounds.extents" + col.bounds.extents);
Debug.Log("col.bounds.extents.x" + col.bounds.extents.x);
Debug.Log("col.size" + col.size);
Debug.Log("col.center" + col.center);
}
}
}
col.boundsCenter: (9.50, 0.91, 4.50), Extents: (0.50, 0.50, 0.50)
- boundsCenter : center + position, Extents : size의 절반
col.bounds.extents(0.50, 0.50, 0.50)
col.bounds.extents.x0.5
col.size(1.00, 1.00, 1.00)
col.center(0.00, 0.00, 0.00)
콜라이더 메서드
@ 레이캐스트
- 마우스로 오브젝트를 클릭해서 (레이캐스트쏘고 확인한다음) 움직이게 하기.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private BoxCollider col;
private void Start()
{
col = GetComponent<BoxCollider>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitinfo;
if(col.Raycast(ray, out hitinfo, 1000))
{
this.transform.position = hitinfo.point;
}
}
}
}
}
if (Input.GetMouseButtonDown(0)) : 마우스의 왼쪽 버튼(0번 버튼)이 눌렸는지 확인.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); : 카메라에서 마우스 커서 위치를 통해 레이를 생성.
camera.main : 메인 카메라의 태그가 메인카메라인것.
- this.transform.position = hitinfo.point; : 현재 게임 객체의 위치를 레이와 충돌한 위치로 변경.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class Test : MonoBehaviour
{
private BoxCollider col;
private void Start()
{
col = GetComponent<BoxCollider>();
}
void Update()
{
void OnTriggerEnter(Collider other)
{
}
void OnTriggerExit(Collider other)
{
}
void OnTriggerStay(Collider other)
{
}
}
}
- OnTriggerEnter(Collider other): 다른 Collider가 현재 게임 객체의 Trigger Collider에 처음으로 접촉할 때 호출됩니다.
- OnTriggerExit(Collider other): 다른 Collider가 현재 게임 객체의 Trigger Collider에서 벗어날 때 호출됩니다.
- OnTriggerStay(Collider other): 다른 Collider가 현재 게임 객체의 Trigger Collider 안에 머무르는 동안 매 프레임 호출됩니다.
void OnTriggerStay(Collider other)
{
other.transform.position += new Vector3(0, 0, 10);
}
- 콜라이더에 닿은 오브젝트의 위치가 (0,0,10)으로 점점 밀려남
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class Test : MonoBehaviour
{
private BoxCollider col;
private void Start()
{
col = GetComponent<BoxCollider>();
}
void Update()
{
// Update 메소드 내에 다른 로직을 작성할 수 있습니다.
}
private void OnCollisionEnter(Collision collision)
{
}
private void OnCollisionExit(Collision collision)
{
}
private void OnCollisionStay(Collision collision)
{
}
}
- OnCollisionEnter(Collision collision) 메소드는 게임 객체의 Collider가 다른 Collider와 처음 충돌할 때 호출됩니다.
- collision 매개변수는 충돌에 대한 상세 정보를 담고 있는 Collision 객체입니다. 이 객체는 충돌한 두 객체의 정보, 접촉 지점, 충돌 강도 등을 포함합니다.
- 이 메소드 안에 충돌 시 발생해야 하는 로직을 작성할 수 있습니다. 예를 들어, 충돌 시 점수를 증가시키거나, 게임 객체의 상태를 변경하는 등의 작업을 수행할 수 있습니다.
- OnCollisionExit(Collision collision) 메소드는 게임 객체의 Collider가 다른 Collider와의 충돌에서 벗어날 때 호출됩니다.
- collision 매개변수는 충돌에 대한 상세 정보를 담고 있는 Collision 객체입니다.
- 이 메소드 안에 충돌이 끝났을 때 발생해야 하는 로직을 작성할 수 있습니다. 예를 들어, 충돌이 끝난 후에 특정 효과를 종료하거나, 상태를 리셋하는 작업을 수행할 수 있습니다.
- OnCollisionStay(Collision collision) 메소드는 게임 객체의 Collider가 다른 Collider와 계속 충돌하고 있는 동안 매 프레임 호출됩니다.
- collision 매개변수는 충돌에 대한 상세 정보를 담고 있는 Collision 객체입니다.
- 이 메소드 안에 충돌이 지속되는 동안 반복적으로 발생해야 하는 로직을 작성할 수 있습니다. 예를 들어, 충돌이 지속되는 동안 체력을 감소시키거나, 충돌 효과를 유지하는 등의 작업을 수행할 수 있습니다.
collision과 trigger의 차이
- 충돌(Collision): 충돌 이벤트는 두 개의 Collider가 물리적으로 상호작용할 때 발생합니다. 이 경우 Collider들은 서로 밀어내거나, 물리적으로 반응합니다. 충돌 이벤트는 Rigidbody가 있는 Collider와 다른 Collider 간의 상호작용을 다룹니다.
- 트리거(Trigger): 트리거 이벤트는 Collider가 다른 Collider와 겹칠 때 발생하지만, 물리적으로 상호작용하지 않습니다. 즉, 트리거는 물리적 반응 없이 단순히 겹침을 감지합니다. 트리거로 설정된 Collider는 다른 Collider와 충돌하지 않고, 대신 겹침 이벤트를 발생시킵니다.
- Collision : Collider가 물리적으로 충돌할 때 호출됩니다, Collider가 트리거로 설정되지 않은 경우에만 작동합니다.
- Trigger : Collider가 트리거로 설정된 경우에 호출됩니다, Collider가 트리거로 설정되면, 물리적 충돌은 발생하지 않고 겹침 이벤트만 발생합니다.