박스 콜라이더(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 : 메인 카메라의 태그가 메인카메라인것.
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)
{
}
}
}
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)
{
}
}
collision과 trigger의 차이
- Collision : Collider가 물리적으로 충돌할 때 호출됩니다, Collider가 트리거로 설정되지 않은 경우에만 작동합니다.
- Trigger : Collider가 트리거로 설정된 경우에 호출됩니다, Collider가 트리거로 설정되면, 물리적 충돌은 발생하지 않고 겹침 이벤트만 발생합니다.
카메라(Camera) (0) | 2024.06.25 |
---|---|
MeshRenderer, 마우스 클릭시 오브젝트 색 바꾸기 (0) | 2024.06.25 |
리지드바디(Rigidbody), 이동, 회전, 폭발 (0) | 2024.06.24 |
Transform 오브젝트 이동하기, 회전하기, 크기조절, 바라보기, 공전하기 (0) | 2024.06.22 |
스크립트의 직렬화(SerializeField / Serializable) (0) | 2024.05.30 |