[LayaAir 2.0]射线检测报错,outHitInfo.sprite3D=null;
代码:
class SceneScript extends Laya.Script{
private ray : Laya.Ray;
private hit: Laya.RaycastHit;
private phasorSprite3D:Laya.PhasorSpriter3D;
private camera : Laya.Camera;
private scene: Laya.Scene;
public _load(owner : any):void{
this.scene = owner;
this.camera = this.scene.scene.getChildByName("Main Camera") as Laya.Camera;
console.log("当前摄像机的名字是:" + this.camera.name);
}
public _start(state:Laya.RenderState):void{
this.ray = new Laya.Ray(Laya.Vector3.ZERO,Laya.Vector3.ZERO);
this.phasorSprite3D = new Laya.PhasorSpriter3D();
Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.onMouseDown);
}
private onMouseDown():void{
this.camera.viewportPointToRay(new Laya.Vector2(Laya.MouseManager.instance.mouseX,Laya.MouseManager.instance.mouseY),this.ray);
Laya.Physics.rayCast(this.ray,this.hit);
console.log("当前射线碰撞的信息是:" + this.hit.sprite3D.name);
//摄像机位置
var position:Laya.Vector3=new Laya.Vector3(this.camera.position.x,
0, this.camera.position.z);
//开始绘制矢量3D精灵,类型为线型
this.phasorSprite3D.begin(Laya.WebGLContext.LINES, this.camera);
//根据射线的原点绘制参考直线(为了观察方便而绘制,但矢量线并不是射线真正的路径)
this.phasorSprite3D.line(this.ray.origin, new Laya.Vector4(1,0,0,1), position , new Laya.Vector4(1,0,0,1));
//如果与物品相交,画三面边线
if(this.hit.sprite3D)
{
//从碰撞信息中获取碰撞处的三角面顶点
var trianglePositions:Array<any>= this.hit.trianglePositions;
//矢量绘制三角面边线
this.phasorSprite3D.line(trianglePositions[0], new Laya.Vector4(1,0,0,1),
trianglePositions[1], new Laya.Vector4(1,0,0,1));
this.phasorSprite3D.line(trianglePositions[1], new Laya.Vector4(1,0,0,1),
trianglePositions[2], new Laya.Vector4(1,0,0,1));
this.phasorSprite3D.line(trianglePositions[2], new Laya.Vector4(1,0,0,1),
trianglePositions[0], new Laya.Vector4(1,0,0,1));
}
//结束绘制
this.phasorSprite3D.end();
//如果碰撞信息中的模型不为空,删除模型
if(this.hit.sprite3D)
{
console.log("hankex1");
//从场景中移除模型
this.scene.removeChild(this.hit.sprite3D);
}
}
}
class SceneScript extends Laya.Script{
private ray : Laya.Ray;
private hit: Laya.RaycastHit;
private phasorSprite3D:Laya.PhasorSpriter3D;
private camera : Laya.Camera;
private scene: Laya.Scene;
public _load(owner : any):void{
this.scene = owner;
this.camera = this.scene.scene.getChildByName("Main Camera") as Laya.Camera;
console.log("当前摄像机的名字是:" + this.camera.name);
}
public _start(state:Laya.RenderState):void{
this.ray = new Laya.Ray(Laya.Vector3.ZERO,Laya.Vector3.ZERO);
this.phasorSprite3D = new Laya.PhasorSpriter3D();
Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.onMouseDown);
}
private onMouseDown():void{
this.camera.viewportPointToRay(new Laya.Vector2(Laya.MouseManager.instance.mouseX,Laya.MouseManager.instance.mouseY),this.ray);
Laya.Physics.rayCast(this.ray,this.hit);
console.log("当前射线碰撞的信息是:" + this.hit.sprite3D.name);
//摄像机位置
var position:Laya.Vector3=new Laya.Vector3(this.camera.position.x,
0, this.camera.position.z);
//开始绘制矢量3D精灵,类型为线型
this.phasorSprite3D.begin(Laya.WebGLContext.LINES, this.camera);
//根据射线的原点绘制参考直线(为了观察方便而绘制,但矢量线并不是射线真正的路径)
this.phasorSprite3D.line(this.ray.origin, new Laya.Vector4(1,0,0,1), position , new Laya.Vector4(1,0,0,1));
//如果与物品相交,画三面边线
if(this.hit.sprite3D)
{
//从碰撞信息中获取碰撞处的三角面顶点
var trianglePositions:Array<any>= this.hit.trianglePositions;
//矢量绘制三角面边线
this.phasorSprite3D.line(trianglePositions[0], new Laya.Vector4(1,0,0,1),
trianglePositions[1], new Laya.Vector4(1,0,0,1));
this.phasorSprite3D.line(trianglePositions[1], new Laya.Vector4(1,0,0,1),
trianglePositions[2], new Laya.Vector4(1,0,0,1));
this.phasorSprite3D.line(trianglePositions[2], new Laya.Vector4(1,0,0,1),
trianglePositions[0], new Laya.Vector4(1,0,0,1));
}
//结束绘制
this.phasorSprite3D.end();
//如果碰撞信息中的模型不为空,删除模型
if(this.hit.sprite3D)
{
console.log("hankex1");
//从场景中移除模型
this.scene.removeChild(this.hit.sprite3D);
}
}
}
没有找到相关结果
已邀请:
要回复问题请先登录
2 个回复
Hankex
赞同来自:
console.log("当前射线碰撞的信息是:" + this.hit.sprite3D.name); 没打印
Aar0n
赞同来自: