[LayaAir 2.0]rigidBody.applyForce 物体不会移动

我想让角色使用rigidBody.applyForce 的方式去移动,但是角色在地上一动也不动的


export default class GameUI {
//宣告場景
private scene: Laya.Scene3D;
private camera: Laya.Camera;
private directionlight: Laya.DirectionLight;
private character: Laya.Sprite3D;
private monster_character: Laya.Sprite3D;
private translateW: Laya.Vector3 = new Laya.Vector3(0, 0, 0.2);
private translateS: Laya.Vector3 = new Laya.Vector3(0, 0, -0.2);
private translateA: Laya.Vector3 = new Laya.Vector3(0.2, 0, 0);
private translateD: Laya.Vector3 = new Laya.Vector3(-0.2, 0, 0);
private translateSpace: Laya.Vector3 = new Laya.Vector3(0, 1, 0);
private monster_init_x: number = 0;
private monster_init_y: number = 0;
private monster_init_z: number = 0;
private trans: Laya.Vector3;
private rigidBody: Laya.Rigidbody3D;

private tmp_x: number = 0;
private tmp_y: number = 0;
private tmp_z: number = 0;
private yaw: number = 0;
private pitch: number = 0;
private animator: Laya.Animator;

private point: Laya.Vector2 = new Laya.Vector2();
private _ray: Laya.Ray;
private _outHitResult: Laya.HitResult;
private text: Laya.Text = new Laya.Text();

constructor() {
//初始化引擎
Laya3D.init(0, 0);
Laya.stage.scaleMode = Laya.Stage.SCALE_FULL;
Laya.stage.screenMode = Laya.Stage.SCREEN_NONE;
Laya.Stat.show();
//加载场景人物腳色
Laya.Scene3D.load(
"res/build5/SampleScene.ls",
Laya.Handler.create(this, this.onComplete)
);
//射线初始化(必须初始化)
this._ray = new Laya.Ray(
new Laya.Vector3(0, 0, 0),
new Laya.Vector3(0, 0, 0)
);

//初始化变量
this.point = new Laya.Vector2();
this._outHitResult = new Laya.HitResult();
}

/**
* 加载完成
*/
private onComplete(scene: Laya.Scene3D): void {
this.scene = new Laya.Scene3D();
//将场景加到舞台上
this.scene = scene;
Laya.stage.addChild(scene);

//添加方向光
var directionlight: Laya.DirectionLight = scene.getChildByName(
"Directional Light"
) as Laya.DirectionLight;
this.directionlight = directionlight;

//加入子層人物腳色到舞台
var littleGirl: Laya.Sprite3D = scene.getChildByName(
"Girl"
) as Laya.Sprite3D;
scene.addChild(littleGirl);

this.character = littleGirl;
//獲取動畫組建
this.animator = littleGirl.getComponent(Laya.Animator) as Laya.Animator;
this.playIdle();
this.rigidBody = this.character.getComponent(Laya.Rigidbody3D);

Laya.timer.frameLoop(1, this, this.onKeyDown);

//獲取照相機
var camera: Laya.Camera = littleGirl.getChildByName(
"Main Camera"
) as Laya.Camera;

this.camera = camera;

//新增怪物
this.RandomMonster_x();
this.RandomMonster_z();
var monster_position_x = this.monster_init_x;
var monster_position_z = this.monster_init_z;
var Monster: Laya.Sprite3D = scene.getChildByName(
"Monster1"
) as Laya.Sprite3D;
scene.addChild(Monster);

Monster.transform.translate(
new Laya.Vector3(monster_position_x, 0, monster_position_z)
);
this.monster_character = Monster;
// this.master_turn();
//添加自定义模型
var box: Laya.MeshSprite3D = this.scene.addChild(
new Laya.MeshSprite3D(Laya.PrimitiveMesh.createBox(2, 2, 2))
) as Laya.MeshSprite3D;
box.transform.translate(new Laya.Vector3(5, 5, 5));
box.transform.rotate(new Laya.Vector3(0, 45, 0), false, false);
var material: Laya.BlinnPhongMaterial = new Laya.BlinnPhongMaterial();
Laya.Texture2D.load(
"res/layabox.png",
Laya.Handler.create(null, function (tex: Laya.Texture2D) {
material.albedoTexture = tex;
})
);
box.meshRenderer.material = material;

//设置文本显示框位置
this.text.x = Laya.stage.width / 2 - 50;
//显示文本显示框
this.text = new Laya.Text();
this.text.overflow = Laya.Text.HIDDEN;
this.text.color = "#FFFFFF";
this.text.font = "Impact";
this.text.fontSize = 24;
this.text.x = (Laya.stage.width / 5) * 2;
Laya.stage.addChild(this.text);
Laya.stage.on(Laya.Event.MOUSE_DOWN, this, this.onMouseDown);

let building: Laya.Sprite3D = scene.getChildByName(
"Buliding"
) as Laya.Sprite3D;

let moveArea: Laya.MeshSprite3D = building.getChildByName(
"fence15"
) as Laya.MeshSprite3D;

console.log("-----");
console.log(building);
console.log("-----");
console.log(moveArea);
}

private playRun(): void {
this.animator.play("Running");
this.animator.speed = 1.0;
}
private playIdle(): void {
this.animator.play("Idle");
this.animator.speed = 1.0;
}
private playWalk(): void {
this.animator.play("Walk");
this.animator.speed = 1.0;
}

private onKeyDown(): void {
//開始暂停動畫
this.MonsterMoveToMainCharacter();
// console.log(this.monster_character.transform.position);
//this.animator.speed = 0.0;
let keyW = Laya.KeyBoardManager.hasKeyDown(87);
let keyS = Laya.KeyBoardManager.hasKeyDown(83);
let keyA = Laya.KeyBoardManager.hasKeyDown(65);
let keyD = Laya.KeyBoardManager.hasKeyDown(68);
let keyShift = Laya.KeyBoardManager.hasKeyDown(16);
let haskeyW: boolean = false;
let haskeyS: boolean = false;
let haskeyA: boolean = false;
let haskeyD: boolean = false;
let haskeyShift: boolean = false;

if (keyW) {
if (keyShift) {
this.playRun();
this.rigidBody.applyForce(new Laya.Vector3(0, 0, 0.2));
// this.character.transform.translate(this.translateW);
haskeyShift = true;
} else {
this.playWalk();
this.rigidBody.applyForce(new Laya.Vector3(0, 0, 0.2));
// this.character.transform.translate(this.translateW);
haskeyW = true;
}
}

if (keyS) {
if (keyShift) {
this.playRun();
this.rigidBody.applyForce(new Laya.Vector3(0, 0, -0.2));
// this.character.transform.translate(this.translateS);
haskeyShift = true;
} else {
this.playWalk();
this.rigidBody.applyForce(new Laya.Vector3(0, 0, -0.2));
// this.character.transform.translate(this.translateS);
haskeyS = true;
}
}
if (keyA) {
if (keyShift) {
this.playRun();
this.rigidBody.applyForce(this.translateA);
// this.character.transform.translate(this.translateA);
haskeyShift = true;
} else {
this.playWalk();
this.rigidBody.applyForce(this.translateA);
// this.character.transform.translate(this.translateA);
haskeyA = true;
}
}
if (keyD) {
if (keyShift) {
this.playRun();
this.rigidBody.applyForce(this.translateD);
// this.character.transform.translate(this.translateD);
haskeyShift = true;
} else {
this.playWalk();
this.rigidBody.applyForce(this.translateD);
// this.character.transform.translate(this.translateD);
haskeyD = true;
}
}
if (
haskeyW === false &&
haskeyA === false &&
haskeyS === false &&
haskeyD === false &&
haskeyShift === false
) {
this.playIdle();
}

if (Laya.KeyBoardManager.hasKeyDown(32) === true) {
this.playRun();
this.character.transform.translate(this.translateSpace);
}

if (Laya.KeyBoardManager.hasKeyDown(69) === true) {
this.character.transform.rotate(new Laya.Vector3(0, -5, 0), false, false);
}
if (Laya.KeyBoardManager.hasKeyDown(81) === true) {
this.character.transform.rotate(new Laya.Vector3(0, 5, 0), false, false);
}
}

private onMouseMove(): void {
var mouseY = Laya.MouseManager.instance.mouseY;
var speedY: number = 2;
this.pitch -= speedY * mouseY;
this.character.transform.rotate(
new Laya.Vector3(0, 0.001 * this.pitch, 0),
false,
false
);
}

private onMouseDown() {
this.point.x = Laya.MouseManager.instance.mouseX;
this.point.y = Laya.MouseManager.instance.mouseY;
//產生射线
this.camera.viewportPointToRay(this.point, this._ray);
//拿到射线碰撞的物体
this.scene.physicsSimulation.rayCast(this._ray, this._outHitResult);
//如果碰撞到物体
if (this._outHitResult.succeeded) {
//删除碰撞到的物体
this.text.text = "点击到了" + this._outHitResult.collider.owner.name;
}
}
}
已邀请:

要回复问题请先

商务合作
商务合作