import { _decorator, Camera, Component, geometry, Node, PhysicsSystem, sys, SystemEvent, systemEvent } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('NewComponent')
export class NewComponent extends Component {

    @property(Camera)
    camera: Camera = null;
    start() {
        systemEvent.on(SystemEvent.EventType.TOUCH_START, (e) => {
            this.shexian(e.getLocation().x, e.getLocation().y);
        })
    }

    shexian(x, y) {
        let ray = new geometry.Ray();
        this.camera.screenPointToRay(x, y, ray);
        // 以下参数可选
        const mask = 0xffffffff;
        const maxDistance = 10000000;
        const queryTrigger = true;

        if (PhysicsSystem.instance.raycastClosest(ray, mask, maxDistance, queryTrigger)) {
            const raycastClosestResult = PhysicsSystem.instance.raycastClosestResult;
            const hitPoint = raycastClosestResult.hitPoint
            const hitNormal = raycastClosestResult.hitNormal;
            const collider = raycastClosestResult.collider;
            const distance = raycastClosestResult.distance;

            console.log("collider===>", collider.name)
            // console.log("distance===>", distance)
        }
    }
}

