Unity/코드 예제들
캐릭터 경사면에 서 있는상태 체크(미끄러질수있는 곳)
소나무꼴
2021. 12. 20. 12:18
https://docs.unity3d.com/ScriptReference/RaycastHit-normal.html
Unity - Scripting API: RaycastHit.normal
Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close
docs.unity3d.com
Vector3 pos1 = transform.position;
Vector3 dir = new Vectire(0, -1, 0);
float maxDir = 1;
// 현재 좌표를 기준으로 아래로 Ray를 쏜다.
if (Physics.Raycast(pos1, dir, out RaycastHit raycastHit, maxDis))
{
Debug.Log($"충돌 {raycastHit.transform.name}" );
// 충돌한 지점과 캐릭터의 라인을 만든다
Vector3 incomingVec = raycastHit.point - this.transform.position;
// 만들어진 라인과 충돌된 곳의 Normal을 가지고 반사값을 만들자.
// 중력이 고정되었다면 raycastHit.normal 만 가지고도 경사를 측정할수도 있을꺼 같다..
Vector3 reflectVec = Vector3.Reflect(incomingVec, raycastHit.normal);
// Draw lines to show the incoming "beam" and the reflection.
Debug.DrawLine(this.transform.position, raycastHit.point, Color.red, 0.3f);
Debug.DrawRay(raycastHit.point, reflectVec, Color.green, 0.3f);
}