CREATE YOUR WORLD
Scripting
Raycast
3min
raycast 함수는 unity의 장면 내에서 객체를 효율적으로 찾는 데 사용할 수 있습니다 unity 프로그래밍에서 raycast는 장면의 위치에서 발사되어 collider에 부딪힐 때까지 쏘는 일종의 레이저 빔입니다 collider에 충돌할 때, 충돌한 객체의 정보(raycasthit)와 장면 내 위치가 반환됩니다 unity raycast에 대한 자세한 내용은 unity 가이드를 참조하십시오 📘 unity raycast https //docs unity3d com/manual/camerarays html https //docs unity3d com/manual/camerarays html unity raycast는 다음 단계를 완료하여 zepeto script에서 사용할 수 있습니다 먼저, raycast 관련 클래스를 가져옵니다 import { physics, raycasthit } from 'unityengine'; 다음은 raycast 함수의 예제 코드입니다 raycast import { zepetoscriptbehaviour } from 'zepeto script'; import { physics, raycasthit, input, camera } from 'unityengine'; export default class raycastsample extends zepetoscriptbehaviour { update() { this mousecontrol(); } mousecontrol() { if (input getmousebuttondown(0)) { let ray = camera main screenpointtoray(input mouseposition); // raycasthit 정보를 출력 매개변수로 유지하려면 $ref 태그로 감싸야 합니다 // 매개변수로 out 한정자가 필요하므로 실제 객체를 생성하지 않고 참조를 만듭니다 let ref = $ref\<raycasthit>(); if (physics raycast(ray, ref, 1000)) { // 반환된 raycast hit 데이터를 확인하려면 $unref를 사용하여 참조를 풀어야 합니다 let hitinfo = $unref(ref); console log(`hit 감지!`); console log(`hitinfo collider name ${hitinfo collider name}`); } else { console log(`충돌 감지 실패`); } } } } 📘 스크립트에서 사용된 $ref 및 $unref에 대한 설명은 다음 가이드를 참조하십시오 \[ $ref & $unref docid\ mn4di18ariap mmazk5eb ] 테스트 코드를 통해 장면에서 마우스 포인터로 큐브를 클릭하여 객체의 정보가 출력되는지 확인할 수 있습니다 zepeto script에서 사용된 raycast api에 대한 자세한 정보는 아래 링크를 참조하십시오 📘 raycast https //docs unity3d com/scriptreference/physics raycast html https //docs unity3d com/scriptreference/physics raycast html