https://hillier.tistory.com/14
[Unity] Collision Type 속성
Collision - 충돌collider - 충돌한 GameObject의 Collider Component (읽기전용)contacts - 물리엔진에 의해 생성되는 물체 간의 충돌지점. 접점은 여러개가 될 수 있음 -> 배열 타입 반환 ( ex 첫번째 충돌 지점 = co
hillier.tistory.com
public class GongPlay : MonoBehaviour
{
[SerializeField] float speed = 2.0f;
Rigidbody rb;
Vector3 startPos;
void Start()
{
rb = GetComponent<Rigidbody>();
// 공의 초기 데이터 저장
startPos = transform.position;
// 게임 시작하자마자 움직이고 싶다.
rb.AddForce(-speed, 0f, speed * 0.7f);
}
//벽에 닿았을 때 튕기고 싶다.
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Wall"))
{
// 충돌한 지점의 위치
Vector3 currPos = collision.transform.position;
Vector3 incomVec = currPos - startPos;// 입사각 계산
Vector3 normalVec = collision.contacts[0].normal;//법선벡터
Vector3 reflectVec = Vector3.Reflect(incomVec, normalVec);//반사각계산
reflectVec = reflectVec.normalized;//반사각정규화
rb.AddForce(reflectVec * speed);
}
//다시 현재위치 저장
startPos = transform.position;
}
}
728x90
'유니티' 카테고리의 다른 글
6/21 배열 , 형변환 (0) | 2023.06.28 |
---|---|
6/28 C# (0) | 2023.06.28 |
유니티 화면녹화 (0) | 2023.06.27 |
비주얼 스튜디오 단축키 (0) | 2023.06.27 |
유니티 플레이모드 구분하기 (0) | 2023.06.27 |