[유니티 입문 강좌] part4 컬라이더

kkily·2021년 11월 20일
0

collider: 충돌과 관련된 컴포넌트 , collider가 있기 때문에 충돌했을 때 멈춤


Edit Collider: collider 크기 조절, alt와 shift로 쉽게 조절 가능
size: 크기 조절
center : 중심 조절
material:매질 (탄성도와 관련)
is trigger: 충돌을 하면 감지만 하고 실제 물리적 엔진은 무시, 어떤곳에 닿으면 대화가 실행되고 다른 곳에 출현하고 등 이런 상황에 쓰임


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
   
    //[SerializeField] 
    private BoxCollider col;

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

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.W)){ // W키를 누르면 한번만 발동

        Debug.Log(col.bounds) //콜라이더의 바운딩 볼륨(center, extents)을 보여줌
        Debug.Log(col.bounds.extents) //콜라이더의 각 면의 사이즈의 반을 보여줌
        Debug.Log(col.bounds.extents.x) //콜라이더의 x면의 사이즈의 반을 보여줌
        Debug.Log(col.size) //콜라이더의 사이즈  보여줌
        Debug.Log(col.center) //콜라이더의 중심  보여줌
        
        col.size= new Vector3(3,3,3); //사이즈 수정 가능,bounds는 수정 불가


        }

        if(Input.GetMouseButtonDown(0)){ //마우스 버튼 클릭할 때마다
        //마우스가 있는 좌표에 레이저를 쏨
            Ray ray=Camera.main.ScreenPointToRay(Input.mousePostion); 
            RaycastHit hitInfo;
            if(col.Raycast(ray,out hitInfo,1000)){//레이저 맞았으면
                this.transform.position=hitInfo.point; //마우스 위치로 큐브 옮김
            }

        }
    }
}

메소드 설명

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
   
    //[SerializeField] 
    private BoxCollider col;

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

    
    void OnTriggerEnter(Collider other) 
    //istrigger 체크된 상태에서 box collider 안에 다른 콜라이더가 들어왔다면
    {
      
    }
    void OnTriggerExit(Collider other) {
        //나가는 순간 실행

        other.transform.position+=new Vector3(0,2,0); //빠져나가는 순간 움직임

    }

    void OnTriggerStay(Collider other) {
        //머물때(둘이 닿아 있을 때) 실행
        other.transform.position+=new Vector3(0,0,0.1f); //다른 애가 z축으로 0.1씩 이동 
    }

    private void onCollisionEnter(Collision collision){ //istrigger 체크 안됐고 실제 물리적 충돌이 일어났을 때

    }
    private void onCollisionExit(Collision collision){ 

    }
    private void onCollisionStay(Collision collision){ 

    }
}
profile
낄리의 개발 블로그╰(*°▽°*)╯

0개의 댓글