功能介绍
自定义实现一个游戏中角色的移动效果
一分钟快速上手
核心实现逻辑是在update中,对当前角色添加一个指定方向的向量即可 核心逻辑如下:
/** 当脚本被实例后,会在第一帧更新前调用此函数 */
protected onStart(): void {
this.useUpdate = true;
InputUtil.onKeyPress(Type.Keys.W, () => this._forward = true);
InputUtil.onKeyUp(Type.Keys.W, () => this._forward = false);
InputUtil.onKeyPress(Type.Keys.S, () => this._back = true);
InputUtil.onKeyUp(Type.Keys.S, () => this._back = false);
InputUtil.onKeyPress(Type.Keys.A, () => this._left = true);
InputUtil.onKeyUp(Type.Keys.A, () => this._left = false);
InputUtil.onKeyPress(Type.Keys.D, () => this._right = true);
InputUtil.onKeyUp(Type.Keys.D, () => this._right = false);
Gameplay.asyncGetCurrentPlayer().then((player: Gameplay.Player) => {
if (player) {
this._curCharacter = player.character;
this._curCharacter.movementDirection = Gameplay.MovementDirection.AxisDirection;
}
});
}
protected onUpdate(dt: number): void {
if (this._curCharacter) {
if (this._forward) {
this._curCharacter.movementAxisDirection = Vector.forward;
this._curCharacter.addMoveInput(Vector.forward.multiply(this._moveSpeed));
}
if (this._back) {
this._curCharacter.movementAxisDirection = Vector.back;
this._curCharacter.addMoveInput(Vector.back.multiply(this._moveSpeed));
}
if (this._left) {
this._curCharacter.movementAxisDirection = Vector.left;
this._curCharacter.addMoveInput(Vector.left.multiply(this._moveSpeed));
}
if (this._right) {
this._curCharacter.movementAxisDirection = Vector.right;
this._curCharacter.addMoveInput(Vector.right.multiply(this._moveSpeed));
}
}
}
其中关键点 在于 需要设置角色的 运动依据的轴方向
以下附上完整demo案例 与演示效果
roleCuestomMove.zip
(49.84 KB, 下载次数: 59)
|