[LayaAir 2.0]Cannot read property 'rayCast' of undefined

export default class indexscene extends Laya.Script {

constructor(){
super();

this.rotation = new Laya.Vector3(0, 0.01, 0);
this.point = new Laya.Vector2();
this.ray = new Laya.Ray(new Laya.Vector3(),new Laya.Vector3());
//1.开启第四个参数
let config3D = new Laya.Config3D();
config3D.isAlpha = true;
Laya3D.init(0, 0, config3D);
Laya.stage.scaleMode = Laya.Stage.SCALE_FULL;
Laya.stage.screenMode = Laya.Stage.SCREEN_NONE;
//2.设置舞台背景色为空
Laya.stage.bgColor = null;
this.scene = new Laya.Scene3D();
this.camera = new Laya.Camera(0, 0.1, 100);
Laya.stage.addChild(this.scene);
this.scene.addChild(this.camera);
this.camera.transform.translate(new Laya.Vector3(0, 3.5, 1));
this.camera.transform.rotate(new Laya.Vector3(-15, 0, 0), true, false);
//3.清除照相机颜色
this.camera.clearColor = null;
this.directionLight = new Laya.DirectionLight();
this.scene.addChild(this.directionLight);
this.directionLight.direction = new Laya.Vector3(0, -0.8, -1);
this.directionLight.color = new Laya.Vector3(0.7, 0.6, 0.6);
Laya.Sprite3D.load("res/scene/LayaScene_webgl_sky/Conventional/webgl_sky.lh", Laya.Handler.create(this, this.onLoadFinish));

Laya.stage.on(Laya.Event.MOUSE_DOWN, this, this.mouseDown);
}
onLoadFinish(layaMonkey){
this.layaMonkey=this.scene.addChild(layaMonkey);
// Laya.timer.frameLoop(1, this, this.onFrameLoop);
}
onFrameLoop(){
this.layaMonkey.transform.rotate(this.rotation, false);
}
mouseDown(){
this.point.x = Laya.MouseManager.instance.mouseX;
this.point.y = Laya.MouseManager.instance.mouseY;
//产生射线
this.camera.viewportPointToRay(this.point, this.ray);
var out = new Laya.CannonHitResult();
this.scene.cannonPhysicsSimulation.rayCast(this.ray,out);
if(out.succeeded)
{
console.log(111);
}

}
}
已邀请:

程程

赞同来自:

问题解决了,改一下方法就行了
mouseDown(){
this.point.x = Laya.MouseManager.instance.mouseX;
this.point.y = Laya.MouseManager.instance.mouseY;
//产生射线
this.camera.viewportPointToRay(this.point, this.ray);
var out = new Laya.HitResult();
this.scene.physicsSimulation.rayCast(this.ray,out);
if(out.succeeded)
{
console.log(111);
}

}

Laya_z

赞同来自:

你上面的写法用的是cannon物理引擎,cannon物理引擎和bullet是两套东西,如果你想用cannon引擎,可以参考https://layaair2.ldc2.layabox.com/demo2/?language=zh&category=3d&group=CannonPhysics3D&name=CannonPhysicsWorld_RayCheck,但是cannon引擎现在不支持结合自导出资源使用,所以你的代码不能用cannon引擎来写,只能使用bullet。

要回复问题请先

商务合作
商务合作