[Unity] 콜라이더(Collider)

박호준·2022년 2월 11일
0

Unity

목록 보기
3/17

충돌과 관련된 컴포넌트

  • Edit Collider : 박스로 주변 영역 설정
  • Meterial : 물질 설정
  • Is Trigger : 충돌 감지만 (물리적 충돌x)

기본 속성 출력해보기

public class collider : MonoBehaviour
{
    private BoxCollider col;
    // Start is called before the first frame update
    void Start()
    {
        col = GetComponent<BoxCollider>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            Debug.Log("col.bounds" + col.bounds);
            Debug.Log("col.bounds.extents" + col.bounds.extents);
            Debug.Log("col.bounds.extend.x" + col.bounds.extents.x);
            Debug.Log("col.size" + col.size);
            Debug.Log("col.center" + col.center);

        }
    }
}

마우스 포인터로 다가오기

public class collider : MonoBehaviour
{
    private BoxCollider col;
    // Start is called before the first frame update
    void Start()
    {
        col = GetComponent<BoxCollider>();
    }

    // Update is called once per frame
    void Update()
    {
        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;
            }
        }
    }
}

onStay series[istrigger check(o)]

public class collider : MonoBehaviour
{
    private BoxCollider col;
    // Start is called before the first frame update
    void Start()
    {
        col = GetComponent<BoxCollider>();
    }
    //private void OnTriggerEnter(Collider other) // 들어올때 최초 1회
    //private void OnTriggerExit(Collider other) // 나갈때 최초 1회
    private void OnTriggerStay(Collider other) // 머물때 최초 1회
    {
        other.transform.position += new Vector3(0, 0, 0.01f);
    }
}

[istrigger check(x)]

private void OnCollisionEnter(Collision collision)
private void OnCollisionExit(Collision collision)
private void OnCollisionStay(Collision collision)
profile
hopark

0개의 댓글