射线
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);
}
}
}