Laya 實操十六:射線

射線

export default class Test extends Laya.Script {

    constructor() { 
        super(); 
        //場景
        Laya.Scene3D.load("res/LayaScene_Scene_Test/Conventional/Scene_Test.ls",Laya.Handler.create(this,this.on_scene3d_loaded));
    }

    camera = null;
    scene3d = null;

    on_scene3d_loaded(scene3d){
        Laya.stage.addChild(scene3d);
        this.createPrefab(scene3d);
        this.camera = scene3d.getChildByName("Main Camera");
        this.scene3d = scene3d;
    }

    createPrefab(scene3d){
        Laya.Sprite3D.load("res/LayaScene_Scene_Person/Conventional/Golem.lh",Laya.Handler.create(this,function(prefab){
            scene3d.addChild(prefab);
            prefab.transform.position = new Laya.Vector3(0,0,5);
        }));
    }

    onAwake(){
        Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.on_mouse_down);
    }

    on_mouse_down(){
        var ray = new Laya.Ray(new Laya.Vector3(0,0,0),new Laya.Vector3(1,1,1));

        var screen_pos = new Laya.Vector2(Laya.MouseManager.instance.mouseX,Laya.MouseManager.instance.mouseY);
        this.camera.viewportPointToRay(screen_pos,ray);

        var result = new Laya.HitResult();
        if(this.scene3d.physicsSimulation.rayCast(ray,result)){
            console.log(result.succeeded);
            console.log(result.collider);
            console.log(result.point);
            console.log(result.normal);
            console.log(result.collider.owner.name);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章