CREATE YOUR WORLD
Scripting
Raycast
3min
the raycast function can be used for efficiently finding an object within a scene in unity raycast in unity programming is a kind of laser beam, which will shoot from a location in a scene and shoot until it hits a collider when it collides a collider, the information(raycasthit) of the collided object and position within the scene will be returned for more details on unity raycast, please review the unity guide 📘 unity raycast https //docs unity3d com/manual/camerarays html https //docs unity3d com/manual/camerarays html the unity raycast can be used in zepeto script by completing the following steps first, import the raycast related class import { physics, raycasthit } from 'unityengine'; the following is an example code for the raycast function 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); // please note that in order to retain the raycasthit information as an output parameter it must be wrapped in a $ref tag // because we require the out qualifier as a parameter, we create a reference without actually creating an object let ref = $ref\<raycasthit>(); if (physics raycast(ray, ref, 1000)) { // to check the returned raycast hit data, please remember to unwrap the reference by using $unref let hitinfo = $unref(ref); console log(`detect hit!`); console log(`hitinfo collider name ${hitinfo collider name}`); } else { console log(`failed to detect collision`); } } } } 📘 please refer to the following guide for explanations of the $ref and $unref used in the script \[ $ref & $unref docid\ cwpvooiusl3wotxuhhekj ] through the test code, you can check that the information of the object is output by clicking the cube with the mouse pointer in the scene find more information for the raycast api used in the zepeto script by going to the link below 📘 raycast https //docs unity3d com/scriptreference/physics raycast html https //docs unity3d com/scriptreference/physics raycast html