[]碰撞检测 outHitInfo.distance 一直等于 -1 ?(TS代码)
if(this.outHitInfo.distance < 0)speedX = speedZ = 0; 这个判断中的this.outHitInfo.distance 一直等于-1?
--------------------------------
class RoleControlScript extends Laya.Script{
/*角色模型*/
public roleModel:Laya.Sprite3D;
/*角色动画组件*/
public roleAni:Laya.Animator;
/*角色当前动作*/
public currentAction:string = "stand";
/*角色动画是否完成*/
public aniComplete:Boolean = true;
/*角色移动速度*/
public speed:number = 0.04;
/*3D摄像机*/
public camera:Laya.Camera;
/*摇杆控制器*/
private rocker:RockerView;
/*攻击按钮控制器*/
private attack:attackView;
/*摇杆上一帧角度*/
private lastAngle:number = 0;
/*检测移动区碰撞器的射线*/
private ray:Laya.Ray;
/*碰撞检测信息*/
private outHitInfo:Laya.RaycastHit;
constructor() {
super();
this.ray = new Laya.Ray(new Laya.Vector3(0,0,0),new Laya.Vector3(0,-2,0));
this.outHitInfo = new Laya.RaycastHit();
}
/*覆写3D组件方法,指3D对象加载组件时执行
*owner:此组件所属的3D对象
*/
public _load(owner:Laya.Sprite3D):void{
//获取控制器UI
this.rocker = Example_roleControl.rocker;
this.attack = Example_roleControl.attack;
}
/*覆写加载组件的3D对象实例化完成后,第一次更新时执行*/
public _start(state:Laya.RenderState):void{
//获取被绑定脚本的模型,需等待角色实例化完成
//获取有动画组件的内层模型(.lh资源导出时会在角色外包裹一层sprite3D)
this.roleModel = this.owner.getChildByName("girl1") as Laya.Sprite3D;
//模型缩放
this.roleModel.transform.localScale = new Laya.Vector3(0.8,0.8,0.8);
//获取角色动画组件
this.roleAni = this.roleModel.getComponentByType(Laya.Animator) as Laya.Animator;
//动画完成事件监听
this.roleAni.on(Laya.Event.COMPLETE,this,this.onComplete);
}
/*覆写3D组件更新方法(相当于帧循环)*/
public _update(state:Laya.RenderState):void{
//如果是攻击状态播放击球动画(优先播放击球动画)
if(this.attack.isAttack){
if(this.currentAction != "play"){
this.play();
//摄像机移动向量
Example_roleControl.cameraTranslate = new Laya.Vector3(0,0,0);
}
}
//上次击球动画如果未结束,不执行以下代码
if(!this.aniComplete)return;
//如果摇杆有方向角度
if(this.rocker.angle != -1){
//摇杆控制角色旋转方向(笨帧摇杆的角度-上一帧的角度=本帧应当旋转的角度)
this.roleModel.transform.rotate(new Laya.Vector3(0,this.rocker.angle - this.lastAngle,0),false,false);
//通过弧度和速度计算角色在x,z轴上移动的量
var speedX:number = Math.sin(this.rocker.radians) * this.speed;
var speedZ:number = Math.cos(this.rocker.radians) * this.speed;
//记录角色本帧的角度
this.lastAngle = this.rocker.angle;
//行走区域碰撞检测,如未与行走区域模型碰撞,则不移动
//射线原点
var rayOrigin:Laya.Vector3 = new Laya.Vector3(0,0,0);
var ownerSprite3D: Laya.Sprite3D = this.owner as Laya.Sprite3D ;
//根据角色位置计算射线原点
Laya.Vector3.add(ownerSprite3D.transform.position,new Laya.Vector3(speedX,2,speedZ),rayOrigin);
//射线原点位置更新
this.ray.origin = rayOrigin;
//物理射线与碰撞器相交检测
Laya.Physics.rayCast(this.ray,this.outHitInfo,5);
//------------------------------------------------TODO_WPP 碰撞器生成有问题?? 应该是outHitInfo.distance -1 这个问题
// //如果未有碰撞则返回
// if(this.outHitInfo.distance < 0)speedX = speedZ = 0; 这里会一直小于0 等于-1
//更新角色位置
ownerSprite3D.transform.translate(new Laya.Vector3(speedX,0,speedZ),false);
//播放行走动画
if(this.currentAction != "go")this.go();
}
else{
//如果摇杆未有角度则播放待机动画
if(this.currentAction != "stand")this.stand();
}
//摄像机移动向量
//注:因为克隆需求,所以提供移动向量给主类,由主类控制摄像机更新。
//如果只有单一主角,可以直接在脚本中控制摄像机移动。
Example_roleControl.cameraTranslate = new Laya.Vector3(speedX,0,speedZ);
}
/*动画播放完成回调*/
private onComplete():void{
//角色动画完成
this.aniComplete = true;
//如果结束的动画剪辑名为play,则播放站立待机动画
if(this.roleAni.currentPlayClip.name == "play") this.stand();
}
/*角色行走动画*/
public go():void{
this.roleAni.play("go",1.4);
this.currentAction = "go";
}
/*角色待机动画*/
public stand():void{
this.roleAni.play("stand");
this.currentAction = "stand";
}
/*角色击球动画*/
public play():void{
this.roleAni.play("play");
this.currentAction = "play";
this.aniComplete = false;
}
}
没有找到相关结果
已邀请:
2 个回复
ymsdandan
赞同来自:
ymsdandan
赞同来自: